js 中有關toFixed的用法

定義和用法

toFixed() 方法可把 Number 四捨五入爲指定小數位數的數字。

 

語法

NumberObject.toFixed(num)

 

參數           描述

num           必需。規定小數的位數,是 0 ~ 20 之間的值,包括 0 和 20,有些實現可以支持更大的數值範

                  圍。如果省略了該參數,將用 0 代替。(注意:num的範圍0 ~ 20之間).

 

實例

在本例中,我們將把數字舍入爲僅有一位小數的數字:

Show the number 13.37 with one decimal:
<script type="text/javascript">
var num = new Number(13.37);
document.write (num.toFixed(1))
</script>

輸出:

Show the number 13.37 with one decimal:
13.4

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章