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