TypeScript - Number 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))

編譯後,它將在 JavaScript 中生成相同的程式碼。

程式碼將產生以下輸出:

num3.toFixed() is 177 
num3.toFixed(2) is 177.23 
num3.toFixed(6) is 177.234000
typescript_numbers.htm
廣告