Number toFixed() Method - Javascript Number

Javascript examples for Number:toFixed

Description

The toFixed() method converts a number into a string, keeping a specified number of decimals.

Syntax

number.toFixed(x)

Parameter Values

Parameter Description
x Optional. The number of digits after the decimal point. Default is 0 (no digits after the decimal point)

Return Value:

A String, representing a number, with the exact number of decimals

The following code shows how to Convert a number into a string:

Demo Code

ResultView the demo in separate window

<!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>

Related Tutorials