How to round up to the nearest 100 in JavaScript

JavascriptMath

Javascript Problem Overview


I want to round up to the nearest 100 all the time whether or not the value is 101 or 199 it should round up to 200. For example:

var number = 1233;
//use something like Math.round() to round up to always 1300

I'd like to always round up to the nearest 100, never round down, using jQuery.

Javascript Solutions


Solution 1 - Javascript

Use Math.ceil(), if you want to always round up:

Math.ceil(number/100)*100

Solution 2 - 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

Solution 3 - Javascript

No part of this requires jQuery. Just use JavaScript's Math.ceil:

Math.ceil(x / 100) * 100

Solution 4 - Javascript

This is an easy way to do it:

((x/100).toFixed()*100;

Solution 5 - Javascript

Simplest solution would be:

Math.round(number/100)*100

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCurtisView Question on Stackoverflow
Solution 1 - JavascriptMightyPorkView Answer on Stackoverflow
Solution 2 - JavascriptSergey GurinView Answer on Stackoverflow
Solution 3 - Javascriptuser229044View Answer on Stackoverflow
Solution 4 - JavascriptJoe BannounaView Answer on Stackoverflow
Solution 5 - JavascriptVikrant BhatView Answer on Stackoverflow