Gustavo Sanchez Gustavo Sanchez - 5 months ago 15
Javascript Question

Round number to nearest thousand, up or down depending on the number

I want to round up a number to the nearest thousand, at the moment I'm using this:

Math.ceil(value/1000)*1000;


But this goes always up, if I type 1001 it will go to 2000, I want to go up or down depeding on the number, for example 1001 goes to 1000 or 1400 goes to 1000 but 1500 goes to 2000

EDIT:

if(value<1000){
value = 1000;
}else{
value = Math.round(value/1000)*1000;
}

Answer

This will do what you want:

Math.round(value/1000)*1000

examples:

Math.round(1001/1000)*1000
1000
Math.round(1004/1000)*1000
1000
Math.round(1500/1000)*1000
2000
Comments