Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript toFixed() Method

JavaScript toFixed() Method

Number Object Reference JavaScript Number object

Example

Converts a number to a string, with a specified number of digits after the decimal point:

var num = 5.56789;

var n=num.toFixed(2);

n Output result:

Try it »


Definition and Usage

The toFixed() method converts a floating-point number to a string representation with a specified number of decimal places, padding with 0 if the number of decimal places exceeds the number.

The toFixed() method rounds a Number to a specified number of decimal places.

**Note:** The toFixed() method may produce inaccurate results when rounding, because it actually uses round-to-nearest-even (banker's rounding) when handling floating-point numbers, rather than standard rounding. This strategy reduces cumulative error in large computations.

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 value

Parameters Description
x Required. Specifies the number of decimal places, a value between 0 and 20 inclusive; some implementations support a larger range. If omitted, 0 is used.

Return Value

Type Description
String A numeric string.

Technical Details

JavaScript version: 1.5

More examples

Example

Convert a number with no decimal places:

var num = 5.56789;

var n=num.toFixed();

n Output result:

Try it »

Example

Convert a number with fewer decimal places than specified:

var num = 5.56789;

var n=num.toFixed(10);

n Output result:

Try it »

The toFixed() method uses round-to-nearest-even (banker's rounding) for floating-point numbers rather than standard rounding, as shown below:

Example

let num1

=

0.615

;

console.

log

(

num1.

toFixed

(

2

)

)

;

// Output "0.61" instead of "0.62"

For more accurate rounding, other methods can be used, such as multiplying the number by a factor, calling the Math.round() function, and handling decimal places manually, as shown 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

)

)

;

// Output "0.62"


Number Object Reference JavaScript Number object