DecimalFormat numform = new DecimalFormat("000.0"); System.out.println( "Num = " + numform.format(13.456) );
Num = 013.5
value of double | format pattern | output string |
---|---|---|
123.456 | "000.000" | "123.456" |
123.456 | "000.0" | "123.5" |
123.456 | "000" | "123" |
89.008 | "000.000" | "089.008" |
89.008 | "0000.0000" | "0089.0080" |
89.008 | "0.00" | "89.01" |
89.008 | "0." | "89." |
The pattern 000.00
asks for 6 characters, three digits on the left,
a decimal separator,
and two digits on the right.
However, all the digits of the integer part of the number
will be output, regardless of the format pattern.
This table shows how some 64-bit double
s are
converted to strings (using the US locale)
for various format patterns.
The quote marks are not part of the output string. They are there to show the start and end of the string.
What does the following fragment write?
DecimalFormat numform = new DecimalFormat("000.0"); System.out.println( "Num = " + numform.format(1.19) );