Hard-coded 8191 10485 values in JavaScript rounding function

Posted by Matthew Hegarty on Stack Overflow See other posts from Stack Overflow or by Matthew Hegarty
Published on 2010-03-19T15:01:23Z Indexed on 2010/03/19 15:11 UTC
Read the original article Hit count: 295

Filed under:

I've seen the following (bizarre) Javascript rounding function in some legacy code. After googling for it I can see that it crops up in a number of places online. However I can't work out why the hard-coded values 8191 and 10485 are present.

Does anyone know if there's any sensible reason why these values are included? If not, hopefully we can kill off the meme!

function roundNumber(num,dec) {
    var newnumber = 0;
    if (num > 8191 && num < 10485) {
        num = num-5000;
        newnumber = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
        newnumber = newnumber+5000;
    } else {
        newnumber = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    }
    return newnumber;
}

© Stack Overflow or respective owner

Related posts about JavaScript