【发布时间】:2019-03-08 11:40:16
【问题描述】:
当我尝试打印带有更多小数点的浮点数时,我遇到了一种非常奇怪的行为。
Float price = Float.valueOf("1602.72");
System.out.println(price); //prints 1602.72
outputValue = String.format("%.6f", price);
System.out.println(outputValue); //prints 1602.719971
我也使用了下面的代码,但得到了相同的结果
DecimalFormat df = new DecimalFormat("0.000000");
System.out.println(df.format(price)); //prints 1602.719971
我希望 outputValue 为 1602.720000(小数点后 6 位,带有额外的零)
【问题讨论】:
-
那么,没有办法实现尾随零吗?
-
与此密切相关,即使它是专门为 android 标记的:stackoverflow.com/q/35673034/4636715 所以,考虑使用
BigDecimal。 -
所以没有办法在浮点数和十进制中做到这一点。我现在将 0 附加到字符串。谢谢
标签: java floating-point decimal