开发者

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

开发者 https://www.devze.com 2023-01-13 15:16 出处:网络
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

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

精彩评论

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