Hard-coded 8191 10485 values in JavaScript rounding function
- by Matthew Hegarty
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;
}