zoukankan      html  css  js  c++  java
  • js 字符串转数字

    Number()、parseInt()、parseFloat()的区别: 

    Number()的强制类型转换与parseInt()和parseFloat()方法的处理方式相似,只是它转换的是整个值,而不是部分值。parseInt()和parseFloat()方法只转换第一个无效字符之前的字符串。如“3.4.5”被转换成“3.4”, 

    用Number()进行强制类型转换将返回NAN, 
    如果字符串值能被完整地转换,Number()将判断是调用parseInt()还是parseFloat()。 

    1. var bb = "35.23ace23";   
    2. document.write(Number(bb));                    //NaN   
    3. document.write(parseFloat(bb));                //35.23   
    4. document.write(parseFloat(Number(bb)));        //NaN  

     
    1. var bb = "35.23ace23";  
    2. document.write(Number(bb));                    //NaN  
    3. document.write(parseFloat(bb));                //35.23  
    4. document.write(parseFloat(Number(bb)));        //NaN  




    Number.toFixed(x) 、 Number.toPrecision(x) 、 Math.Round(x)的区别: 

    Number.toFixed(x) 是将指定数字截取小数点后 x 位, Number.toPrecision(x) 是将整个数字截取指定(x)长度。 
    注意:一个是计算小数点后的长度,一个是计算整个数字的长度 。 
    Math.round() 方法可把一个数字舍入为最接近的整数。 

     
      1. var aa = 2.3362;    
      2. document.write(aa.toFixed(1));                 // 2.3    
      3. document.write(aa.toFixed(2));                 // 2.34    
      4. document.write(aa.toPrecision(2));             // 2.3   
      5. document.write(aa.toPrecision(3));             // 2.34     
      6. document.write(Math.round(-4.60));             // -5   
      7. document.write(Math.round(aa * 10) / 10);      // 2.3    
      8. document.write(Math.round(aa * 100) / 100);    // 2.34    

    http://blog.csdn.net/liuzhen917/article/details/8490219

  • 相关阅读:
    zoj 3627#模拟#枚举
    Codeforces 432D Prefixes and Suffixes kmp
    hdu 4778 Gems Fight! 状压dp
    CodeForces 379D 暴力 枚举
    HDU 4022 stl multiset
    手动转一下田神的2048
    【ZOJ】3785 What day is that day? ——KMP 暴力打表找规律
    poj 3254 状压dp
    C++中运算符的优先级
    内存中的数据对齐
  • 原文地址:https://www.cnblogs.com/ljbkyBlog/p/7767911.html
Copyright © 2011-2022 走看看