给定示例代码:
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