[ACCEPTED]-JavaScript flooring number to order of magnitude-javascript

Accepted answer
Score: 33

So you're looking for the order of magnitude.

function convert(n) {
    var order = Math.floor(Math.log(n) / Math.LN10
                       + 0.000000001); // because float math sucks like that
    return Math.pow(10,order);
}

Simple 4 ^_^ Math is awesome! Floating point imprecisions, however, are 3 not. Note that this won't be completely 2 accurate in certain edge cases, but it will 1 do its best.

More Related questions