Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何在 Java 中舍入到特定的倍数?在 excel 中,有一个mround函数可以轻松舍入到指定的倍数,如下所示:
mround
mRound(variable,multiple)
所以mRound(x,3)会返回9ifx = 7.9和6if x = 7.2。
mRound(x,3)
9
x = 7.9
6
x = 7.2
到目前为止,我发现的所有舍入函数总是四舍五入到最接近的整数或指定的小数位数,但我希望能够更改每个变量的倍数。有谁知道什么功能最适合这种情况?
只需除以数字,四舍五入,然后乘以数字。
double mRound(double value, double factor) { return Math.round(value / factor) * factor; }