The toFixed method in JavaScript is utilized to return a string that represents a number with a specified number of digits following the decimal point, rather than in exponential form.
Syntax
The syntax for the toFixed method is illustrated as follows:
Example
Number.toFixed(num)
Parameter
num - This parameter indicates the quantity of digits that should be displayed following the decimal point.
Return
A string that denotes a numerical value with a precise number of digits following the decimal point.
JavaScript Number toFixed method example
In this section, we will explore the toFixed method by examining several examples.
Example 1
Let's see a simple example of toFixed method.
Example
<script>
var a=98.9721;
document.writeln(a.toFixed());
</script>
Output:
Example 2
Let’s examine an example of how to round a number to a specified number of decimal places.
Example
<script>
var a=98.9721;
document.writeln(a.toFixed(2));
</script>
Output: