本文共 884 字,大约阅读时间需要 2 分钟。
本文翻译自:
If the value is 200.3456
, it should be formatted to 200.34
. 如果值为200.3456
,则应将其格式化为200.34
。 If it is 200
, then it should be 200.00
. 如果是200
,则应为200.00
。
参考:
For two rounding digits. 对于两个舍入数字。 Very simple and you are basically updating the variable instead of just display purposes which DecimalFormat does. 非常简单,您基本上是在更新变量,而不仅仅是DecimalFormat的显示目的。
x = Math.floor(x * 100) / 100;
如果您确实想要相同的双精度数,但以所需的方式四舍五入,则可以使用BigDecimal,例如
new BigDecimal(myValue).setScale(2, RoundingMode.HALF_UP).doubleValue();
value = (int)(value * 100 + 0.5) / 100.0;
double d = 28786.079999999998;String str = String.format("%1.2f", d);d = Double.valueOf(str);
double value= 200.3456;DecimalFormat df = new DecimalFormat("0.00"); System.out.println(df.format(value));
转载地址:http://bvcnb.baihongyu.com/