JavaScript Rounding to The Next Nearest Digit Number

This is actually just a modification from JavaScript Round To Nearest Number by Talk in Code. Instead of getting the nearest digit, we will actually get the rounding to the next nearest number.

function roundNextNearest(num, acc) {
    if ( acc < 0 ) {
        return Math.round(num*acc)/acc;
    } else {
        var value = Math.round(num/acc)*acc;
        if (value < num) {
            value += acc;
        }
    }
    return value;
}
Posted in Code Snippet, JavaScript | Leave a comment

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>