Num = 098,765.43
Use the character # in the format string
to show a digit that will be omitted from the output string
if it is a leading
or trailing zero.
For integers:
the pattern has two halves.
It starts with any number of #s,
followed by one or more 0s.
Here are some legal patterns:
####0 ###00 ##0 000 ###0
For floating point:
The integer part of the pattern
follows the rules for integers.
If a decimal point is included,
the pattern for the fraction
has two halves: it
starts with any number of 0s
followed by any number of #s.
Here are some legal patterns:
####0.## ###00.## ##0.00## ##0.000 000 ###0 ##0.
Here are some illegal patterns:
#### --- needs to end with at least one 0 ###.## --- integer part needs to end with a 0 ##0.0##00 --- bad fractional part (can't surround #s with 0s) ##00## --- bad integer pattern ###0.##0 --- bad fractional part
Usually format() produces a reasonable string,
even with defective format patterns.
But seriously defective patterns cause it to throw an
IllegalArgumentException at run time.
The compiler does not inspect the format patterns.
Here are a few examples:
| value of double | format pattern '0' | output string | format pattern "#" | output string |
|---|---|---|---|---|
123.456789 | "0000.000" | "0123.457" | "#.0" | "123.5" |
12345.678 | "000,000" | "012,346" | "#,###" | "12,346" |
Is the following format string correct?
000.###