使用Math.round舍入到一个小数位? [英] Using Math.round to round to one decimal place?
本文介绍了使用Math.round舍入到一个小数位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这两个变量
double num = 540.512
double sum = 1978.8
然后我做了这个表达
double total = Math.round((num/ sum * 100) * 10) / 10;
但我最终得到27.0。
but I end up with 27.0.
事实上我有很多其他变量,当我在表达式中使用它时,我总是在第十个位置得到一个。
In fact I have many other variables and when I use them in the expression I always get a 0 in the tenth's place.
推荐答案
我刚才创建的有用方法...
Helpful method I created a while ago...
private static double round (double value, int precision) {
int scale = (int) Math.pow(10, precision);
return (double) Math.round(value * scale) / scale;
}
这篇关于使用Math.round舍入到一个小数位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文