JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

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 programs shows some examples of how to format numbers to strings:

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:

  • The factory methods getInstance() and getNumberInstance() return DecimalFormat objects with the same formatting pattern.
  • Rounding is used to remove the extra digits in the fraction part.
  • If leading extra digits in the integer part will show up in the output string, no error, or truncation, will be given.
  • The percent sign in pattern will convert the number by multiplying with 100.
  • The locale sensitive currency character will be replaced by the specific currency sign of country setting in the default locale.

Sections in This Chapter

java.util.NumberFormat - Formatting Numeric Values to Strings

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

Dr. Herong Yang, updated in 2008
java.util.NumberFormat - Formatting Numeric Values to Strings