Javascript -- toFixed()函数

Javascript -- toFixed()函数

1. toFixed(n) 限制小数点后位数,四舍五入。n:0~20 。

2. 作用对象必须是number,不能为其他类型。如(8.001).toFixed(2)返回8.00;

3. toFixed(n)返回值String类型,所有当成数字进行比大小是错误的。

4. parseFloat(“number“)与parseInt("number")读取字符串中第一个遇到的数(如91.2w3 第一个数为91.2)并转换为float或int,返回类型为number.

 

语法

 

parseFloat() 将一个字符串转换成一个浮点数字

 

toFixed() 方法可把 Number 四舍五入为指定小数位数的数字

 

参数

 

      parseFloat() 参数为待转换的字符串

 

toFixed() 规定小数的位数,是 0 ~ 20 之间的值,包括 0 和 20,有些实现可以支持更大的数值范围。如果省略了该参数,将用 0 代替

 

返回值

 

      转换后的数字

1 <!DOCTYPE html>
 2 <html>
 3   <script src="http://code.jquery.com/jquery-latest.js"></script>
 4 <head>
 5   <meta charset="utf-8">
 6   <title>toFixed()</title>
 7 </head>
 8 <body>
 9 </body>
10 <script type="text/javascript">
11   var a0 = 8.01.toFixed()
12   var a1 = parseFloat("8.006").toFixed(2);
13   var a2 = parseFloat("9.091").toFixed(2);
14   document.write("a0类型: "+typeof(a0)+"<br>");
15   document.write("a1类型: "+typeof(a1)+"<br>");
16   document.write("a2类型: "+typeof(a2)+"<br>");
17   document.write("a0: "+a0+"<br>");
18   document.write("a1: "+a1+"<br>");
19   document.write("a2: "+a2+"<br>");
20   document.write("a1 is less than a2 ? : " + (a1 < a2) + "<br>");
21 </script>
22 </html>

 

posted @ 2016-08-22 11:10  侠岚之弋痕夕  阅读(357)  评论(0编辑  收藏  举报
Where is the starting point, we don't have a choice, but the destination where we can pursue!