How to round up to the nearest 100 in JavaScript
To round up and down to the nearest 100 use Math.round
:
Math.round(number/100)*100
round
vs. ceil
:
Math.round(60/100)*100 = 100
vs.Math.ceil(60/100)*100 = 100
Math.round(40/100)*100 = 0
vs.Math.ceil(40/100)*100 = 100
Math.round(-60/100)*100 = -100
vs.Math.ceil(-60/100)*100 = -0
Math.round(-40/100)*100 = -0
vs.Math.ceil(-40/100)*100 = -0
No part of this requires jQuery. Just use JavaScript's Math.ceil
:
Math.ceil(x / 100) * 100
Use Math.ceil()
, if you want to always round up:
Math.ceil(number/100)*100