Javascript math. LOL

How much is 77.9 – 70? Usually it is 7.9. Right?

But not so in Javascript. In Javascript it isΒ 7.900000000000006. Funny as hell πŸ™‚

You can “fix” this by calling .toFixed() on the result number, but it does not fix the cause. As far as I know you can’t fix it, it is part of the language. Read more on StackOverflow

Just for case that your browser doesn’t support the .toFixed() method here it is.

if (!Number.prototype.toFixed) {
    Number.prototype.toFixed = function(value, precision) {
        ///<summary>Returns fixed number of decimal values</summary>
        var power = Math.pow(10, precision || 0);
        return String(Math.round(value * power) / power);
    };
}

Try it in your console πŸ™‚