Search code examples
javarounding

Rounding to the closest 100


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


Solution

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

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

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