• 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.

     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>
    1 输出值:
    2 a0类型: string
    3 a1类型: string
    4 a2类型: string
    5 a0: 8
    6 a1: 8.01
    7 a2: 9.09
    8 a1 is less than a2 ? : true  ps:8.01 vs 9.09
  • 相关阅读:
    Hadoop- Cluster Setup
    Hadoop- Cluster Setup
    【网络协议】动态主机配置协议DHCP
    【网络协议】动态主机配置协议DHCP
    数据流(任务并行库 TPL)
    数据流(任务并行库 TPL)
    js数据存储.html
    对象操作(2).html
    对象操作(1).html
    对象forin循环.html
  • 原文地址:https://www.cnblogs.com/nkxyf/p/3956653.html
Copyright © 2020-2023  润新知