java.util.NumberFormat - Formatting Numeric Values to Strings

This section provides a tutorial example on how to use java.util.NumberFormat and java.util.DecimalFormat classes to convert or format numeric values into text strings in various locales.

The Number class, java.lang.Number, is an abstract class representing a numerical value. It has several concrete subclasses: Byte, Short, Integer, Long, Float, and Double.

The NumberFormat class, java.text.NumberFormat, is an abstract class providing a foundation for the derived subclasses to format numbers to strings and parse strings back to numbers.

The DecimalFormat class, java.text.DecimalFormat, is a concrete subclass of NumberFormat. An object of DecimalFormat contains a formatting pattern and locale information. It can be used to format numbers into string representations; or parsing strings for numbers.

There are some factory methods in NumberFormat class that return predefined default DecimalFormat objects with commonly used formatting patterns. For example, NumberFormat.getCurrencyInstance() returns a DecimalFormat object with a pattern of "\u00A4#,##0.00;(\u00A4#,##0.00)" good for formatting a number into a currency representation.

The following program shows some examples of how to format numbers to strings:

/* NumberFormatTest.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
import java.util.*;
import java.text.*;
class NumberFormatTest {
   public static void main(String[] a) {
      testDefault();
      myFormats();
   }
   public static void testDefault() {
      NumberFormat nf;
      double x = -1234.5678;
      nf = NumberFormat.getInstance();
      System.out.println("Default format: "
         + ((DecimalFormat)nf).toPattern() + " -> " + nf.format(x));
      nf = NumberFormat.getNumberInstance();
      System.out.println("Default number format: "
         + ((DecimalFormat)nf).toPattern() + " -> " + nf.format(x));
      nf = NumberFormat.getCurrencyInstance();
      System.out.println("Default currency format: "
         + ((DecimalFormat)nf).toPattern() + " -> " + nf.format(x));
      nf = NumberFormat.getPercentInstance();
      System.out.println("Default percent format: "
         + ((DecimalFormat)nf).toPattern() + " -> " + nf.format(x));
   }   
   public static void myFormats() {
      double myNumber = -1234.5678;
      // getting a formater for default locale
      DecimalFormat df = new DecimalFormat("#,##0.00");
      System.out.println("Pattern: " + df.toPattern()
         + " -> " + df.format(myNumber));

      // locale sensitive currency symbole
      df = new DecimalFormat("\u00A4#,##0.00"); 
      System.out.println("Pattern: " + df.toPattern()
         + " -> " + df.format(myNumber));

      // speical negative subpattern
      df = new DecimalFormat("\u00A4#,##0.00;(\u00A4#,##0.00)"); 
      System.out.println("Pattern: " + df.toPattern()
                  + " -> " + df.format(myNumber));

      // percentage
      df = new DecimalFormat("#,##0.00%"); 
      System.out.println("Pattern: " + df.toPattern()
         + " -> " + df.format(myNumber));

      // locale sensitive currency symbole
      Locale.setDefault(Locale.JAPAN);
      df = new DecimalFormat("\u00A4#,##0.00"); 
      System.out.println("Pattern (Japan): " + df.toPattern()
         + " -> " + df.format(myNumber));
      Locale.setDefault(Locale.JAPAN);
   }
}

Output:

Default format: #,##0.### -> -1,234.568
Default number format: #,##0.### -> -1,234.568
Default currency format: ñ#,##0.00;(ñ#,##0.00) -> ($1,234.57)
Default percent format: #,##0% -> -123,457%
Pattern: #,##0.00 -> -1,234.57
Pattern: ñ#,##0.00 -> -$1,234.57
Pattern: ñ#,##0.00;(ñ#,##0.00) -> ($1,234.57)
Pattern: #,##0.00% -> -123,456.78%
Pattern (Japan): ñ#,##0.00 -> -?1,234.57

Note that:

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

Number Object and Numeric String Conversion

java.util.NumberFormat - Formatting Numeric Values to Strings

 java.util.DecimalFormat.parse() - Parsing Strings to Number Objects

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials

 References

 PDF Printing Version