How can I get Double.toString () behavior and thousands separator?

I want to add a thousands separator to a double variable. I 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 a value of 1234.5:

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

+4
source share
1 answer

The NumberFormat class knows the decimal separator that will be used for your custom locale.

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

You can use the setMaximumFractionDigits method if it gives you too many decimal places.

+4
source

All Articles