Java Reference
In-Depth Information
Table 5-2. DecimalFormat pattern characters
Character
Meaning
Numeric digit (leading zeros suppressed)
#
Numeric digit (leading zeros provided)
0
.
Locale-specific decimal separator (decimal point)
,
Locale-specific grouping separator (comma in English)
Locale-specific negative indicator (minus sign)
-
Shows the value as a percentage
%
Separates two formats: the first for positive and the second for negative values
;
'
Escapes one of the above characters so it appears
Anything else Appears as itself
The NumFormatDemo program uses one DecimalFormat to print a number with only two
decimal places and a second to format the number according to the default locale:
/** A number to format */
public
public static
double intlNumber = 1024.25 ;
/** Another number to format */
public
static final
final double
public static
static final
final double
double ourNumber = 100.2345678 ;
NumberFormat defForm = NumberFormat . getInstance ();
NumberFormat ourForm = new
new DecimalFormat ( "##0.##" );
// toPattern() will reveal the combination of #0., etc
// that this particular Locale uses to format with!
System . out . println ( "defForm's pattern is " +
(( DecimalFormat ) defForm ). toPattern ());
System . out . println ( intlNumber + " formats as " +
defForm . format ( intlNumber ));
System . out . println ( ourNumber + " formats as " +
ourForm . format ( ourNumber ));
System . out . println ( ourNumber + " formats as " +
defForm . format ( ourNumber ) + " using the default format" );
Search WWH ::




Custom Search