Skip to content

JavaScript parseFloat 2 decimals | Example code

  • by

Use toFixed() with parseFloat to get output numbers up to 2 decimals in JavaScript.

parseFloat(yourString).toFixed(2)

If you need performance (like in games), use the math round method.

Math.round(number * 100) / 100

It’s about 100 times as fast as parseFloat(number.toFixed(2))

JavaScript parseFloat 2 decimals

Simple example code.

 <!DOCTYPE html>
 <html>
 <body>

  <script>
    var str = "100";
    var twoPlacedFloat = parseFloat(str).toFixed(2);

    console.log(twoPlacedFloat);
  </script>

</body>
</html> 

Output:

JavaScript parseFloat 2 decimals

Do comment if you have any doubts or suggestions on this JS parseFloat topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading