如何自定义舍入形式

Rap*_* MM 3 java math decimal rounding digits

我的问题可能看起来很简单,但仍然无法得到有效的东西。我需要自定义 Math.round 舍入格式或其他格式以使其工作如下:

如果数字是 1.6 ,他应该四舍五入到 1 ,如果大于或等于 1.7 ,他应该四舍五入到 2.0 。因此,对于所有其他带有 # .6 的小数结果,我将 1.6 四舍五入为 2 的方式应四舍五入为 1。

我怎样才能做到这一点?

谢谢你!

Arc*_*ord 5

只需这样做:

double threshold = 0.7;
Math.round(x - threshold + 0.5);
Run Code Online (Sandbox Code Playgroud)