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

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