Round up to nearest

javascript

Solution

Assuming all values are positive integers:

function roundUp(x){

    var y = Math.pow(10, x.toString().length-1);

    x = (x/y);
    x = Math.ceil(x);
    x = x*y;
    return x;
}

Live Demo: http://jsfiddle.net/fP7Z6/

Problem

If the number is 37, I would like it to round to 40, if the number is 1086, I would like it to round to 2000. If the number is 453992, I would like it to round to 500000. I don't really know how to describe this more generally, sorry, but basically, the number in the highest place should always round up to the nearest digit, and the rest become zeros. I know how to round stuff normally, I just don't know how to cleanly deal with the variation among number of digits. Thanks, Edit: I deleted the 4 to 10 round, because that one seemed not to fit with the rest, and its not really necessary.

Original source