四舍五入在Java中使用机器人的方法Math.round到6位小数 [英] Rounding to 6 decimal places using Math.round method in Java android
本文介绍了四舍五入在Java中使用机器人的方法Math.round到6位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用
double i2 = value * 2.23694;
i2 = (double)(Math.round(i2 * 100)) / 100;
四舍五入双打。但是,四舍五入到只有2位小数。
for rounding doubles. But it rounds to only 2 decimal places.
我想这是6位小数。
有没有办法使用 Math.round
,并有6位小数?
Is there any way to use Math.round
and have 6 decimal places?
推荐答案
正在铸造的东西整数
■哪些会毁了任何舍入。要使用双击
S,用小数点(即 100.0
而不是 100 $的C $ C>)。如果你有6小数想的那样,使用
1000000.0
是这样的:
You are casting things to Integer
s which will ruin any rounding. To use double
s, use a decimal point (i.e 100.0
instead of 100
). And if you want it with 6 decimals, use 1000000.0
like this:
double i2 = value * 2.23694;
i2 = Math.round(i2*1000000.0)/1000000.0;
不过一般我觉得 DecimalFormat的是一个更优雅的解决方案(猜你想它只是四舍五入以present吧)
But generally I think DecimalFormat is a more elegant solution (guessing you want it rounded only to present it):
DecimalFormat f = new DecimalFormat("##.000000");
String formattedValue = f.format(i2);
这篇关于四舍五入在Java中使用机器人的方法Math.round到6位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文