Have you performed simple arithmetic operations like 0.1 + 0.2? You might have gotten something strange: 0.1 + 0.2 = 0.30000000000000004.
Have you performed simple arithmetic operations like 0.1 + 0.2? You might have gotten something strange: 0.1 + 0.2 = 0.30000000000000004.
If you are adding 0.1 + 0.2, then it means you can cut off anything after the first digit (after the dot off course). Because the rest of the 0.1 is only 0 and the rest of 0.2 is 0. That can help with rounding errors on floating point calculations. I don’t program JavaScript, so no idea what the best way to go about it would be.
How would you implement this in code?
I don’t have much JavaScript experience, but maybe
.toFixed()
will help here. Playground (copy the below code to the playground to test): https://playcode.io/javascriptconst number = 0.1 + 0.2 const fixed = number.toFixed(3) // Update header text document.querySelector('#header').innerHTML = message // Log to console console.log(number) console.log(fixed)
outputs: