Java round to next 5000

996 Views Asked by At

I am looking for some java math.round formula which converts next to nearest 5000 value. Suppose if the value is 15555, this should convert to 20000. If the value is 18555, this should also convert to 20000 (since this should give the next 5000 range). So far I am trying this:

Math.round(value/ 5000.0) * 5000.0)

but if value is 15555, this gives me 15000. I want this to be 20000

2

There are 2 best solutions below

2
Lukas Eder On BEST ANSWER

You're then looking for Math.ceil(), not Math.round()

Math.ceil(value/ 5000.0) * 5000.0

Compare also nearest integer function with floor and ceiling functions

0
bereal On

I'd probably prefer using basic integer operations for that (assuming value is a non-negative integer):

int remainder = value % 5000;
int result = remainder == 0 ? value : value - remainder + 5000;