How can I use NumberFormat
to format a double
value for a given Locale
(default locale is sufficient) and for a given number of decimal places?
For example, I have these values:
double d1 = 123456.78;
double d2 = 567890;
And I want to print them in the following way, for example with US locale and 2 decimal places:
123,456.78
567,890.00
I don't actually care about rounding mode in this case, because these double values are gained from a BigDecimal
with the given scale, so their number of decimal places is always lesser or equal than the number of decimal places I want to print.
Edit:
To make things a bit more clear, the thing is that I want to display money in a locale dependent way, but with a fixed number of decimal places. The example above showed, how the values should be formatted if the system locale is en_US
. Now lets say, the system locale is cs_CZ
(Czech), so the same numbers should be formatted in this way:
123 456,78
567 890,00
Now, how can I set up a NumberFormat to always display 2 decimal places, but to display thousands separator and decimal point based on the current locale?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…