Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. Number .toFixed() / toPrecision()

Number .toFixed() / toPrecision()

Methods for formatting numeric output. You can specify the number of decimal places, the number of significant digits, or convert to exponential notation.

Syntax

// Converts to a string with the specified number of decimal places.
var str = number.toFixed(digits);

// Converts to a string with the specified number of significant digits.
var str = number.toPrecision(digits);

// Converts to a string in exponential notation.
var str = number.toExponential(digits);

Method List

MethodDescription
number.toFixed(digits)Returns a string with the decimal part rounded to the specified number of digits. If digits is omitted, 0 is used, resulting in an integer string.
number.toPrecision(digits)Returns a string with the specified total number of significant digits. If digits is omitted, the number is converted to a string as-is.
number.toExponential(digits)Returns a string in exponential notation. The argument specifies the number of digits in the fractional part.

Sample Code

var price = 1234.5678;

// toFixed() specifies the number of decimal places.
console.log(price.toFixed(2)); // Outputs "1234.57". Rounded at the third decimal place.
console.log(price.toFixed(0)); // Outputs "1235". Rounded to the nearest integer.
console.log(price.toFixed(6)); // Outputs "1234.567800". Padded with zeros to fill missing digits.

// toPrecision() specifies the total number of significant digits.
console.log(price.toPrecision(6)); // Outputs "1234.57". Six significant digits in total.
console.log(price.toPrecision(4)); // Outputs "1235". Four significant digits in total.
console.log(price.toPrecision(2)); // Outputs "1.2e+3". Switches to exponential notation when digits are insufficient.

// toExponential() converts to exponential notation.
console.log(price.toExponential(2)); // Outputs "1.23e+3".
console.log(price.toExponential()); // Outputs "1.2345678e+3".

// Practical example: displaying a price
var amount = 980;
console.log(amount.toFixed(2)); // Outputs "980.00". Useful for displaying prices with tax.

// Calculating sales tax
var tax = 980 * 0.1;
console.log(tax); // Outputs "98.00000000000001". Floating-point rounding error occurs.
console.log(tax.toFixed(0)); // Outputs "98". Round to an integer to eliminate the error.

Notes

number.toFixed() is the most commonly used number formatting method. It is useful whenever you need to align decimal places, such as when displaying prices or calculation results. Note that the return value is a string. If you need to use the result in further calculations, convert it back to a number using Number() or parseFloat().

JavaScript numbers are internally represented as IEEE 754 floating-point values, which can cause rounding errors — for example, 0.1 + 0.2 evaluates to 0.30000000000000004. number.toFixed() is commonly used to hide such errors when displaying values on screen.

number.toPrecision() controls the total number of significant digits, making it useful when the magnitude of a number is not known in advance and you want consistent precision. number.toExponential() is used for scientific calculations or when displaying very large or very small numbers.

Browser Compatibility

Chrome Chrome
49+
Supported in all versions
Firefox Firefox
57+
Supported in all versions
Safari Safari
18+
1 or earlier ×
Edge Edge
80+
11 or earlier ×
IE IE
11+
4.5 or earlier ×
Opera Opera
48+
6 or earlier ×
iOS Safari iOS Safari
18+
Supported in all versions
Android Browser Android Browser
37+
4 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .