ES6 - toFixed() 方法

此方法将数字格式化为小数点右侧具有特定位数的数字。

语法

number.toFixed( [digits] )

参数详细信息

  • digits − 小数点后显示的位数。

返回值

不使用指数表示法且小数点后具有精确位数的数字的字符串表示形式。

示例

var num3 = 177.234
console.log("num3.toFixed() is "+num3.toFixed())
console.log("num3.toFixed(2) is "+num3.toFixed(2))
console.log("num3.toFixed(6) is "+num3.toFixed(6))

输出

num3.toFixed() is 177
num3.toFixed(2) is 177.23
num3.toFixed(6) is 177.234000