Javascript Round Up To Hundred

TAGs: 2 Comment

Posted By: eligeske on in JQuery

Round up a number to the nearest one hundred. We are using the modulus to accomplish this easy task.

So I was doing some stuff for a graph that needed some rounding up. Of course as usual my first thing is to google what I am looking for. But today, google had nothing for me. Probably because it was so frikken simple all other programmers don’t bother with putting it somewhere for lazy people like me to find.

function roundUpToHundred($n){
return (($n%100) > 0)?$n-($n%100) + 100:$n;

Here is what is going on with the simple function.

First we toss in the number, $n and we are running a short IF statement. We are using the MODULUS against the number ā€œ$nā€ and 100, which gives us the remainder. If the remainder is 0 ($n%100) > 0, it is already to a hundred and return the $n with no changes. If the $n is greater than 0, we do $n-($n%100) + 100. This is the number ā€“ (remainder) + 100. Gives us the next hundred above our number.

2 Responses to "Javascript Round Up To Hundred"

  1. The website is superb

  2. Math.round(val/100)*100

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Learning DHTMLX Suite UI

Learn the foundation of the DHTMLX Suite quickly while building a single page application with multiple components in harmony.

Popular post