ES6 - toPrecision()



此方法返回一個字串,該字串表示具有指定精度的數字物件。

語法

number.toPrecision( [ precision ] )

引數詳情

  • precision − 指定有效數字位數的整數。

返回值

返回一個字串,表示以定點或指數記數法表示的 Number 物件,四捨五入到 precision 個有效數字。

示例

var num = new Number(7.123456);
console.log(num.toPrecision());
console.log(num.toPrecision(1));
console.log(num.toPrecision(2));

輸出

7.123456
7
7.1
廣告
© . All rights reserved.