Skip to content

JavaScript toFixed return number

  • by

The return value of toFixed() is a string representing the given number using fixed-point notation. You can use parseFloat to parse your string into a float again.

parseFloat(val.toFixed())

JavaScript toFixed return number

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>

    var n = 100;
    var res = parseFloat(n.toFixed(2));
    
    console.log(res);
    console.log(typeof(res))

  </script>
</body>
</html>

Output:

JavaScript toFixed return number

Or

var myNum = 19.5678; // declare number

console.log(myNum.toFixed(2)); // 19.57

Default behavior of toFixed() method by not passing any argument to the toFixed() method:

var myNum = 19.5678; // declare number

console.log(myNum.toFixed()); // 19

Do comment if you have any doubts or suggestions on this Js toFixed() method topic.

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

OS: Windows 10

Code: HTML 5 Version

Tags:

Leave a Reply

Your email address will not be published.

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