JavaScript Number toFixed() method

definition and usage

toFixed() method converts number to string.

toFixed() The method rounds the string to the specified decimal places.

Note:if the decimal places are higher than the number, add zeros.

instance

Example 1

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

try it yourself

Example 2

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

try it yourself

Example 3

round to 10 decimal places:

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

try it yourself

syntax

number.toFixed(digits)

parameter

parameter description
digits

optional. Decimal places. Values between 0 ~ 20, including 0 and 20.

default is 0 (no decimal).

return value

type description
string with (or without) decimal numbers.

technical details

return value

return number string representation, not using exponential notation, with a fixed decimal point after digits digits.

if necessary, the number will be rounded or zero-filled to reach the specified length.

if digits if greater than le+21, then this method only calls number.toString(), ana gina littafin da ke amfani da kwayar yadda a baya.

throw

exception description
RangeError

wanda digits tai yau ko wanda ya kai tsaye kashi.

0 ~ 20 之间 wanda ba kashi na shirya. Daga kade, wasu kuma suka suka kashi yau wanda ya kai tsaye ko 0 ~ 20. Daga kade, wasu kuma suka suka kashi yau wanda ya kai tsaye ko 0 ~ 20.

TypeError wanda baiyar da kasa wanda ba Number ne, ana shirya kashi.

browser support

Number.constructor wani ne ECMAScript3 (ES3) irinshi.

所有浏览器都完全支持 ES3 (JavaScript 1999):

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Tukuru Tukuru Tukuru Tukuru Tukuru Tukuru