English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript toFixed() Method

 JavaScript Number Object

toFixed()The method uses fixed-point notation to format numbers.

If necessary, numbers will be rounded, and if necessary, the decimal part will be filled with zeros to have the specified length.

Grammar:

number.toFixed(digits)
var num = 12345.6789;
num.toFixed(2);
Test and see‹/›

Browser Compatibility

All browsers fully support the toFixed() method:

Method
toFixed()IsIsIsIsIs

Parameter Value

ParameterDescription
digits(Optional) the number of digits that appear after the decimal point; this may be a 0 to2values between 0 and, and the implementation may support larger ranges of values. If this parameter is omitted, it will be considered as 0

Technical Details

Return value:Represent the string of the given number in fixed-point notation
Exception cases:
  • RangeError-If the number is too small or too large. Between 0 and10between 0 (including 0 and10The value of (0) will not cause a RangeError

  • TypeError-If this method is called on a non-Number object

JavaScript Version:ECMAScript 3

More Examples

Convert numbers without retaining any decimal places:

var num = 12345.6789;
num.toFixed();
Test and see‹/›

If necessary, pad the decimal part with zeros:

var num = 12345.6789;
num.toFixed(10);
Test and see‹/›

 JavaScript Number Object