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

  • @aubeynarf
    link
    16 hours ago

    How would you implement this in code?

    • @[email protected]
      link
      fedilink
      26 hours 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