四舍五入到最接近的 100

问题描述:

如何将整数四舍五入到最接近的 100?例如,497 将舍入为 500,98 将舍入为 100,1423 将舍入为 1400.

How do you round an integer to the closest 100? For example 497 would round to 500, 98 round to 100, 1423 round to 1400.

我会除以 100,然后再乘以:

I'd divide by 100, round, and then multiply again:

int initial = ...;
int rounded = (int) Math.round(initial/100.0) * 100;

注意要除以 100.0 而不是 100,所以你用浮点运算进行除法.

Note to divide by 100.0 and not 100, so you do the division in floating point arithmetic.