Javascript examples for Number:toFixed
The toFixed() method converts a number into a string, keeping a specified number of decimals.
number.toFixed(x)
Parameter | Description |
---|---|
x | Optional. The number of digits after the decimal point. Default is 0 (no digits after the decimal point) |
A String, representing a number, with the exact number of decimals
The following code shows how to Convert a number into a string:
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {//from ww w .j av a2s. c o m var num = 5.56789; var n = num.toFixed(2) document.getElementById("demo").innerHTML = n; } </script> </body> </html>