How To Use Number Object toFixed In JavaScript

In this article I am going to explain about Number Object toFixed method JavaScript.
  • 2076

Use toFixed method in JavaScript

toFixed method is used to fixed the specified number of decimal, and convert a number into string.

This method added to create the desired decimal length in javaScript.

All browsers support number object toFixed method.

Syntax

number.toFixed(x)

Example

<!DOCTYPE html>

<html>

<body style ="background-color:red">

 

<p id="demo">Display the fixed number.</p>

 

<button onclick="myFunction()">Click here</button>

 

<script type="text/javascript">

    function myFunction()

    {

        var num = 5.56789;

        var x = document.getElementById("demo");

        x.innerHTML = num.toFixed(2);

    }

</script>

 

</body>

</html>

Output1

op1.jpg

Output2

 op2.jpg

Further Readings

You may also want to read these related articles :here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.