JDK Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.32, 2006

Formatting and Parsing Numbers

Part:   1   2 

JDK Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Internationalization

Character Set and Encoding

Socket Communication

Document Object Model (DOM)

XSD Validation in Java

XSL - Transformer in Java

JCA - Private and Public Key Pairs

JCE - Secret Key

SSL (Secure Socket Layer)

SSL - Client Authentication

... Table of Contents

Sample programs listed in this chapter have been tested with JDK 1.3.1, 1.4.1 and 1.5.0.

Formatting Numbers to Strings

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);
   }
}

(Continued on next part...)

Part:   1   2 

Dr. Herong Yang, updated in 2006
JDK Tutorials - Herong's Tutorial Notes - Formatting and Parsing Numbers