DecimalFormat Objects

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

Default Locale = en_GB
value = 123,456,789

The language is english and the country is Great Britain. Locales specify both language and country.

DecimalFormat Objects

The program creates a DecimalFormat object. The format() method of this object converts a number into a StringBuffer. A StringBuffer is almost the same as a String except it can be modified. No modification of a StringBuffer will be done in this chapter, so it will be treated exactly as if it were a String.

The default locale determines the format of this StringBuffer. The number may be a primitive (like int or double) or a wrapper object (like Integer or Double).

import java.util.Locale;
import java.text.*;

class IODemo2
{
  public static void main ( String[] args )
  {
    int value = 123456789 ;
    
    System.out.println( "Default Locale = " + Locale.getDefault() );
    DecimalFormat decform = new DecimalFormat();   
    System.out.println( "value = " + decform.format(value) );
  }
}

The program uses the default behavior of DecimalFormat.format(). Later you will see methods that further affect the output of format().

QUESTION 3:

(Review :) What three characters are used to separate the digits of an integer into groups of three?