Home
<!DOCTYPE html> <html> <body> <h1>Demo: Floating-point Numbers in JavaScript</h1> <p id="p1">5.1 + 5.2 = </p> <p id="p2">10.1 + 10.2 = </p> <p id="p3">(10.1*100 + 10.2*100)/100 = </p> <script> var f1 = 5.1 + 5.2; //will be 10.3 var f2 = 10.1 + 10.2; //will be 20.299999999999997 var f3 = (10.1*100 + 10.2*100)/100; //instead of 10.1 + 10.2 document.getElementById("p1").textContent += f1; document.getElementById("p2").textContent += f2; document.getElementById("p3").textContent += f3; </script> </body> </html>
Result: