views:

39

answers:

1

I want to add the thousands separator to a variable of type double. I have tried using String.format("%,f", x); and similar, but it seems to have a fixed number of decimal places, unlike Double.toString().

For example, with the value 1234.5:

Double.toString(): 1234.5
String.format(): 1.234,500000
Desired: 1.234,5

+2  A: 

The NumberFormat class knows the decimal separator to use for your user's locale.

NumberFormat formatter = NumberFormat.getInstance();
String formattedDouble = formatter.format(1234.5);

You may use the setMaximumFractionDigits method if this gives you too many decimal places.

zneak
Thanks, `NumberFormat` didn't work perfectly, but a subclass of it, `DecimalFormat`, did.
dsfsdfsdf