JavaScript toFixed()Method

Number 对象参考手册JavaScript Number Object

Example

Convert a number to a string, with the result having a specified number of digits after the decimal point:

var num = 5.56789;
var n=num.toFixed(2);

nOutput result:


Try it Yourself »

Definition and Usage

The toFixed() method converts a floating-point number into a string representation with a specified number of decimal places. If the number of decimal places is higher than the number's precision, it pads with 0s.

The toFixed() method can round a Number to a number with a specified number of decimal places.

Note:The toFixed() method may produce some inaccurate results when rounding, because when handling floating-point numbers, it actually uses a round-to-nearest-even (banker's rounding) strategy rather than standard rounding. This rounding strategy is intended to reduce cumulative errors during large-scale calculations.

var numObj = 12345.6789;

numObj.toFixed();         // 返回 "12346":进行四舍六入五看情况,不包括小数部分
numObj.toFixed(1);        // 返回 "12345.7":进行四舍六入五看情况

numObj.toFixed(6);        // 返回 "12345.678900":用 0 填充

(1.23e+20).toFixed(2);    // 返回 "123000000000000000000.00"

(1.23e-10).toFixed(2);    // 返回 "0.00"

2.34.toFixed(1);          // 返回 "2.3"

2.35.toFixed(1)           // 返回 '2.4'  向上舍入

2.55.toFixed(1)           // 返回 '2.5' 向下舍入,参考以上注意事项

-2.34.toFixed(1);         // 返回 -2.3(由于操作符优先级,负数不会返回字符串)

(-2.34).toFixed(1);       // 返回 "-2.3"(若用括号提高优先级,则返回字符串)

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the toFixed() method


Syntax

number.toFixed(x)

Parameter Values

Parameter Description
x Required. Specifies the number of decimal places, a value between 0 and 20, including 0 and 20. Some implementations may support a larger range of values. If this parameter is omitted, it will be replaced with 0.

Return Value

Type Description
String A string representation of the number.

Technical Details

JavaScript Version: 1.5


More Examples

Example

Round a number without keeping any decimals:

var num = 5.56789;
var n=num.toFixed();

nOutput result:


Try it Yourself »

Example

Convert a number that has fewer digits after the decimal point than the specified number:

var num = 5.56789;
var n=num.toFixed(10);

nOutput result:


Try it Yourself »

When the toFixed() method handles floating-point numbers, it actually uses a round-to-nearest-even (banker's rounding) strategy rather than standard rounding, as shown in the example below:

Example

let num1 = 0.615;
console.log(num1.toFixed(2)); // Outputs "0.61" instead of "0.62"

If more precise rounding is needed, you can use other methods, such as multiplying the number by a certain factor, calling the Math.round() function, and manually handling the decimal places, as shown in the example below:

Example

function roundNumber(number, decimals) {
  const factor = 10 ** decimals;
  return Math.round(number * factor) / factor;
}

let num2 = 0.615;
console.log(roundNumber(num2, 2)); // Outputs "0.62"


Number 对象参考手册JavaScript Number Object

Other Extensions