That depends on where you are. Some possible choices are
123,456,789 123.456.789 123 456 789
In Canada, UK, and US, commas are used to separate groups of three digits. Other countries use periods or spaces for that purpose.
import java.util.Locale; import java.text.*; class IODemo { 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) ); } }
When the Java virtual machine starts running on a computer, it creates an object called the default locale. This object affects the format of strings created from data. Depending on the default locale, a program creates different strings for the same number. On my computer (in the US) the above program writes
Default Locale = en_US value = 123,456,789
The number has been formatted correctly for the locale in which the program is running.
The static method Locale.getDefault()
asks the virtual machine
for a reference to its default locale object.
The println()
statement uses that object's
toString()
method to produce: en_US
.
This specifies that the default language is English and the default country is US. Your computer might output a different locale code and may format the number differently.
Recall that a static method can be run without instantiating an object of the class.
So the static method getDefault()
can be run by using Locale.getDefault()
.
The program uses a DecimalFormat
object.
Its format()
method converts a number
into an appropriately formatted String
.
The default locale determines the format.
The number may be a primitive (such as int
or double
)
or a wrapper object (such as Integer
or Double
).
In Great Britain the program prints:
Default Locale = en_GB value = 123,456,789
The language is english and the country is Great Britain. Locales specify both language and country.
The program uses the default behavior of format()
.
Later you will see methods that further
affect the output of format()
.
(Review :) What characters separate groups of three digits, depending on your location?