toFixed() Method – How to Fix the Number of Decimal Digits
toFixed() specifies the number of digits browsers should show after a number’s decimal point.
Syntax of the toFixed()
Method
toFixed()
accepts only one optional argument. Here is the syntax:
number.toFixed(totalFractionDigits);
The totalFractionDigits
argument specifies the number of digits browsers should show after the number’s decimal point. If omitted, browsers will use 0
—which means it will add no digits after the decimal point.
Examples of the toFixed()
Method
Below are examples of the toFixed()
method.
Fix 703.59
’s decimals to zero digits
const number = 703.59;
number.toFixed(); // Equivalent to number.toFixed(0)
// The invocation above will return: 704
Fix 703.59
’s decimals to one digit
const number = 703.59;
number.toFixed(1);
// The invocation above will return: 703.6
Fix 703.59
’s decimals to three digits
const number = 703.59;
number.toFixed(3);
// The invocation above will return: 703.590
Fix 703.59’s decimals to seven digits
const number = 703.59;
number.toFixed(7);
// The invocation above will return: 703.5900000