3

I created a round function and it works fine in simple rounding.

If I round two numbers A and B, and then I add them, the result is fine.

But, if I "subtract" them, the result is weird. It is not rounded like it should.

I show you the function:

function round(x, p) {
    return parseFloat(x.toFixed(p));
};

Weird result:

var a = 5.0007;
var b = 2.048
var result = round(5.0007, 3) - round(2.048, 2);

// expected result: 2.951
// obtained result: 2.9510000000000005

Here is a JSFiddle!

Would you please help me understand why this subtraction shows a weird result?

Thanks

joaorodr84
  • 1,251
  • 2
  • 14
  • 33

0 Answers0