十进制格式舍入 [英] DecimalFormat rounding
本文介绍了十进制格式舍入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
float floater = 59.999f;
DecimalFormat df = new DecimalFormat(00.0);
System.out.println(df.format(float));
打印:
60.0
我希望打印
59.9
我需要什么在使用 DecimalFormat
之前添加下面这行: / p>
df.setRoundingMode(RoundingMode.DOWN);
查看其他舍入模式,看哪一个最适合您。
注意:此方法仅适用于JDK 1.6或更高版本
Here is small code to illustrate what I am seeing
float floater = 59.999f;
DecimalFormat df = new DecimalFormat("00.0");
System.out.println(df.format(floater));
This prints:
60.0
I would like it to print
59.9
What do I need to do?
解决方案
Add this line before using the DecimalFormat
:
df.setRoundingMode(RoundingMode.DOWN);
Take a look at the other rounding modes and see which one is best for you.
Note : this method works only in JDK 1.6 or above
这篇关于十进制格式舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文