5

在 Java 中,如何舍入到任意值?具体来说,我想四舍五入到 0.0025 步,即:

0.032611 -> 0.0325

0.034143 -> 0.0350

0.035233 -> 0.0350

0.037777 -> 0.0375

...

任何想法或库?

4

2 回答 2

20
y = Math.round(x / 0.0025) * 0.0025
于 2009-01-28T10:16:56.007 回答
2

你可以这样做:

double step = 0.0025;
double rounded = ((int)(unrounded / step + 0.5)) * step;
于 2009-01-28T10:18:04.880 回答