// http://books.google.com/books?id=fvA7zLEFWZgC&pg=PA61&lpg=PA61#v=onepage&q&f=false

function nice_number(num, floor) {
    var exp = (Math.log( num )/Math.LN10)|0
    var s = num/Math.pow(10, exp), n;
    if (floor) {
        if (s < 1.5) n=1;
        else if (s < 3) n=2;
        else if (s < 7) n=5;
        else n=10;
    } else {
        if (s > 5) n = 10;
        else if (s > 2) n = 5;
        else if (s > 1) n = 2;
        else n = 1;
    }
    return n * Math.pow(10, exp)
}
nice_number(158,1)