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 开发者_StackOverflowdecimal places, unlike Double.toString()
.
For example, with the value 1234.5:
Double.toString()
: 1234.5
String.format()
: 1.234,500000
Desired: 1.234,5The 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.
精彩评论