【发布时间】:2019-05-20 04:50:30
【问题描述】:
给出示例代码:
String format = "{0,number}"; // <- What do I put here?
double value = Math.PI * 1e-10;
System.out.println(value);
System.out.println(new MessageFormat(format, Locale.US).format(new Object[]{value}));
System.out.println(new MessageFormat(format, Locale.FRANCE).format(new Object[]{value}));
我应该使用什么格式字符串来获得任何双精度值的全精度输出并且输出是本地化的?例如当前代码的输出为:
3.1415926535897934E-10
0
0
String.valueOf(double) 正确打印了完整的精度,但它不是消息格式,也没有本地化。消息格式决定该值太小而无法打扰。对于大量数字,结果甚至更糟! MessageFormat 打印 16 个有效数字,然后附加一堆零,这些零不能准确反映存储在双精度中的值。例如。与pi * 1e100 我得到:
3.141592653589793E100
31,415,926,535,897,930,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
31 415 926 535 897 930 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
【问题讨论】:
-
也许:stackoverflow.com/questions/22513607/… 会有所帮助。
标签: java floating-point double locale messageformat