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 中,如何舍入到任意值?具体来说,我想四舍五入到 0.0025 步,即:
0.032611 -> 0.0325
0.034143 -> 0.0350
0.035233 -> 0.0350
0.037777 -> 0.0375
...
任何想法或库?
y = Math.round(x / 0.0025) * 0.0025
你可以这样做:
double step = 0.0025; double rounded = ((int)(unrounded / step + 0.5)) * step;