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

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.