Skip to main content

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.

note

toFixed() is sometimes written as Number.prototype.toFixed() because it is a method of the Number object's prototype property.

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.

note
  • toFixed() returns a string value.
  • toFixed() does not change the original number.
  • Browsers round the returned value if necessary.
  • Browsers add extra zeros if necessary to make up the totalFractionDigits.
  • The totalFractionDigits argument must be an integer between 0 and 100 inclusive. Otherwise, browsers will throw an Uncaught RangeError.

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

Try Editing It

CodeSweetly ads

Master NPM Package Creation

Elevate your skills, boost your projects, and stand out in the coding world.
Learn more

Fix 703.59's decimals to one digit

const number = 703.59;

number.toFixed(1);

// The invocation above will return: 703.6

Try Editing It

Fix 703.59's decimals to three digits

const number = 703.59;

number.toFixed(3);

// The invocation above will return: 703.590

Try Editing It

Fix 703.59's decimals to seven digits

const number = 703.59;

number.toFixed(7);

// The invocation above will return: 703.5900000

Try Editing It

Overview

This article discussed what toFixed() is. We also used examples to see how it works.

Your support matters: Buy me a coffee to support CodeSweetly's mission of simplifying coding concepts.

Tweet this article