JavaScript Number toFixed()

The syntax of the toFixed() method is:

num.toFixed(digits)

Here, num is a number.


Number toFixed() Parameters

The toFixed() method takes in:

  • digits (Optional) - Value between 0 and 20 representing the number of digits to appear after the decimal point. By default, it is 0.

Return value from Number toFixed()

  • Returns a String representing the given number using fixed-point notation.

Note: The toFixed() method throws a RangeError if digits is not in between 1 and 100.


Example: Using Number toFixed()

let num = 57.77583;

console.log(num.toFixed()); // 58-> rounded off, no fractional part
console.log(num.toFixed(1)); // 57.8
console.log(num.toFixed(7)); // 57.7758300 -> Added zeros
console.log(num.toFixed(2)); // 57.78

console.log((5.68e20).toFixed(2)); // 568000000000000000000.00
console.log((1.23e-10).toFixed(2)); // 0.00
console.log((-2.34).toFixed(1)); // -2.3

Output

58
57.8
57.7758300
57.78
568000000000000000000.00
0.00
-2.3

Recommended Readings:

Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community