Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Number
  6. toFixed()

Number.prototype.toFixed()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

ThetoFixed() method ofNumber values returns a string representing this number usingfixed-point notation with the specified number of decimal places.

Try it

function financial(x) {  return Number.parseFloat(x).toFixed(2);}console.log(financial(123.456));// Expected output: "123.46"console.log(financial(0.004));// Expected output: "0.00"console.log(financial("1.23e+5"));// Expected output: "123000.00"

Syntax

js
toFixed()toFixed(digits)

Parameters

digitsOptional

The number of digits to appear after the decimal point; should be a value between0 and100, inclusive. If this argument is omitted, it is treated as0.

Return value

A string representing the given number using fixed-point notation. Scientific notation is used if the number's magnitude (ignoring sign) is greater than or equal to 1021 (same return value asNumber.prototype.toString()).

Exceptions

RangeError

Thrown ifdigits is not between0 and100 (inclusive).

TypeError

Thrown if this method is invoked on an object that is not aNumber.

Description

ThetoFixed() method returns a string representation of a number without usingexponential notation and with exactlydigits digits after the decimal point. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.

If the absolute value of the number is greater or equal to 1021, this method uses the same algorithm asNumber.prototype.toString() and returns a string in exponential notation.toFixed() returns"Infinity","NaN", or"-Infinity" if the value of the number is non-finite.

The output oftoFixed() may be more precise thantoString() for some values, becausetoString() only prints enough significant digits to distinguish the number from adjacent number values. For example:

js
(1000000000000000128).toString(); // '1000000000000000100'(1000000000000000128).toFixed(0); // '1000000000000000128'

However, choosing adigits precision that's too high can return unexpected results, because decimal fractional numbers cannot be represented precisely in floating point. For example:

js
(0.3).toFixed(50); // '0.29999999999999998889776975374843459576368331909180'

Examples

Using toFixed()

js
const numObj = 12345.6789;numObj.toFixed(); // '12346'; rounding, no fractional partnumObj.toFixed(1); // '12345.7'; it rounds upnumObj.toFixed(6); // '12345.678900'; additional zeros(1.23e20).toFixed(2); // '123000000000000000000.00'(1.23e-10).toFixed(2); // '0.00'(2.34).toFixed(1); // '2.3'(2.35).toFixed(1); // '2.4'; it rounds up(2.55).toFixed(1); // '2.5'// it rounds down as it can't be represented exactly by a float and the// closest representable float is lower(2.449999999999999999).toFixed(1); // '2.5'// it rounds up as it's less than Number.EPSILON away from 2.45.// This literal actually encodes the same number value as 2.45(6.02 * 10 ** 23).toFixed(50); // '6.019999999999999e+23'; large numbers still use exponential notation

Using toFixed() with negative numbers

Because member access has higherprecedence than unary minus, you need to group the negative number expression to get a string.

js
-2.34.toFixed(1); // -2.3; a number(-2.34).toFixed(1); // '-2.3'

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-number.prototype.tofixed

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp