我想将一个dobule转换为exaclty两个地方.我有相同查询的答案,但没有一个满足我的问题.
这是我正在使用的代码:
Double d=15.99965754;
System.out.println("Value before formatting is : "+d);
String value=new DecimalFormat("##.00").format(d);
System.out.println("The value after formatting is :" +Double.parseDouble(value));
Run Code Online (Sandbox Code Playgroud)
而我得到的输出是
格式化之前的值为:15.99965754格式化后的值为:16.0
但我真正期待印刷的是:
格式化之前的值为:15.99965754格式化后的值为:16.00
无论双重值是什么,我都应该将小数精确到两个位置.
不解析值,只打印
System.out.println("The value after formatting is :" + value);
Run Code Online (Sandbox Code Playgroud)
获得相同结果的另一种方式
System.out.printf("The value after formatting is : %.2f", d);
Run Code Online (Sandbox Code Playgroud)