如何将数字四舍五入到一定范围内? [英] How to round a number to within a certain range?
本文介绍了如何将数字四舍五入到一定范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的值:
421.18834
而且我必须用一个看起来像这样的掩码来正确地对它进行数学四舍五入:
And I have to round it mathematical correctly with a mask which can look like this:
0.05
0.04
0.1
例如,如果掩码是 0.04,我必须得到值 421.20
,因为 .18 比 .16 更接近 .20.
For example, if the mask is 0.04, i have to get the value 421.20
, because .18 is nearer at .20 than .16.
我使用 Google 发现的所有功能都不起作用.
All functions that I found using Google didn't work.
你能帮我吗?
推荐答案
double initial = 421.18834;
double range = 0.04;
int factor = Math.round(initial / range); // 10530 - will round to correct value
double result = factor * range; // 421.20
这篇关于如何将数字四舍五入到一定范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文