What is the toFixed() method in JavaScript?
The toFixed() method in JavaScript converts a number to a string, rounding it to a specified number of decimals. The functionality of the toFixed method is shown in the illustration below:
Syntax
The toFixed() method is declared as shown below:
Parameters
The toFixed() method takes an optional parameter. This parameter is an integer number that represents the number of digits after the decimal point. If the parameter is unspecified, the default value is zero.
If the number of digits in the parameter is greater than the actual number, the function adds zeroes to the number to create the specified decimal length.
Return value
The toFixed() method returns a string representing the number rounded to the decimal place specified in the parameter.
Examples
The following code shows some examples using the toFixed() method in JavaScript.
//Example 1var num = 2.713678;console.log(num.toFixed());//Example 2console.log(num.toFixed(2));//Example 3console.log(num.toFixed(10));
Free Resources