Have you performed simple arithmetic operations like 0.1 + 0.2? You might have gotten something strange: 0.1 + 0.2 = 0.30000000000000004.

  • thingsiplay@beehaw.org
    link
    fedilink
    arrow-up
    1
    ·
    3 months ago

    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.

      • thingsiplay@beehaw.org
        link
        fedilink
        arrow-up
        2
        arrow-down
        1
        ·
        3 months ago

        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/javascript

        const 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:

        0.30000000000000004
        0.300