-1

I have the following javascript code and it's results are weirding me out.

const value = 50;

const factor1 = 1.1;
const result1 = value * factor1;
console.log(result1) // prints out 55.00000000000001

// when i change the order of the calculation steps, the result i hoped for shows up
const factor2 = 0.1;
const result2 = value + value * factor2;
console.log(result2) // prints out 55

If I take a different start value, say 30, 40, or 60, this problem does not occur. Same when the factor is 1.2. But it does occur when the factor is 1.11. Is there anyway I can prevent this from occurring, besides changing the calculation method as shown above?

martijn
  • 1,417
  • 1
  • 16
  • 26

0 Answers0