Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math.ceil to nearest five at position 1

Okay....

I have a lot of uncontrolled numbers i want to round:

51255 -> 55000
25 -> 25
9214 -> 9500
13135 -> 15000
25123 -> 30000

I have tried modifying the numbers as string and counting length....

But is there a simple way using some Math function maybe?

like image 525
Andreas Louv Avatar asked Sep 13 '11 16:09

Andreas Louv


People also ask

What does math ceil () do?

The Math.ceil() function always rounds up and returns the smaller integer greater than or equal to a given number.

How do you round to the nearest 5 in JavaScript?

To round a number to the nearest 5, call the Math. round() function, passing it the number divided by 5 and multiply the result by 5 .

What is ceil function in JavaScript with example?

ceil() function in JavaScript is used to round the number passed as a parameter to its nearest integer in an Upward direction of rounding i.e towards the greater value. Parameters: This function accepts a single parameter as the number to be rounded to its nearest integer in the upward rounding method.

How do you use floor and ceil in JavaScript?

ceil method returns the smallest integer greater than or equal to the value we pass, Math. floor returns the largest or equal integer that is less than the given value. It also takes a single parameter. So, if we pass the value 1.4 in Math.


2 Answers

Here's my late answer. Uses no Math methods.

function toN5( x ) {
    var i = 5;
    while( x >= 100 ) {x/=10; i*=10;}
    return ((~~(x/5))+(x%5?1:0)) * i;
}

DEMO: http://jsbin.com/ujamoj/edit#javascript,live

   [51255, 24, 25, 26, 9214, 13135, 25123, 1, 9, 0].map( toN5 );

// [55000, 25, 25, 30, 9500, 15000, 30000, 5, 10, 0]

Or this is perhaps a bit cleaner:

function toN5( x ) {
    var i = 1;
    while( x >= 100 ) {x/=10; i*=10;}
    return (x + (5-((x%5)||5))) * i;
}

DEMO: http://jsbin.com/idowan/edit#javascript,live

To break it down:

function toN5( x ) {
   //       v---we're going to reduce x to the tens place, and for each place
   //       v       reduction, we'll multiply i * 10 to restore x later.
    var i = 1;

   // as long as x >= 100, divide x by 10, and multiply i by 10.
    while( x >= 100 ) {x/=10; i*=10;}

   // Now round up to the next 5 by adding to x the difference between 5 and
   //    the remainder of x/5 (or if the remainder was 0, we substitute 5
   //    for the remainder, so it is (x + (5 - 5)), which of course equals x).

   // So then since we are now in either the tens or ones place, and we've
   //    rounded to the next 5 (or stayed the same), we multiply by i to restore
   //    x to its original place.
    return (x + (5-((x%5)||5))) * i;
}

Or to avoid logical operators, and just use arithmetic operators, we could do:

return (x + ((5-(x%5))%5)) * i;

And to spread it out a bit:

function toN5( x ) {
    var i = 1;
    while( x >= 100 ) {
        x/=10; 
        i*=10;
    }
    var remainder = x % 5;
    var distance_to_5 = (5 - remainder) % 5;
    return (x + distance_to_5) * i;
}
like image 89
user113716 Avatar answered Oct 28 '22 10:10

user113716


var numbers = [51255, 25, 9214, 13135, 25123, 3, 6];

function weird_round(a) {
    var len = a.toString().length;
    var div = len == 1 ? 1 : Math.pow(10, len - 2);
    return Math.ceil(a / 5 / div) * div * 5;
}

alert(numbers.map(weird_round));

Also updated for numbers below 10. Won't work properly for negative numbers either, just mention if you need this.

DEMO

like image 40
Alex Turpin Avatar answered Oct 28 '22 11:10

Alex Turpin