开发者

How can I get the Double.toString() behaviour AND thousands separator?

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,5


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜