zhaoxinxin

JS中格式化数据保留两位小数

问题:在JS中格式化数据保留两位小数的函数的多种方法

最好方法:

保留两位好像是这样吧  
  var   a   =   9.39393;  
  alert(a.toFixed(2));

说明:

alert(Number.toFixed(9.39393));  
  返回的是9.39  
  但是只有ie5.5以上的版本才支持。

其它方法:

方法一:

  function   roundFun(numberRound,roundDigit)   //四舍五入,保留位数为roundDigit    
    {  
    if   (numberRound>=0)  
    {  
  var   tempNumber   =   parseInt((numberRound   *   Math.pow(10,roundDigit)+0.5))/Math.pow(10,roundDigit);  
  return   tempNumber;  
  }  
  else    
    {  
  numberRound1=-numberRound  
  var   tempNumber   =   parseInt((numberRound1   *   Math.pow(10,roundDigit)+0.5))/Math.pow(10,roundDigit);  
  return   -tempNumber;  
  }  
            }  

方法二:

<script>  
  tmp   =   "1234567.57232"  
  result   =   tmp.substr(0,tmp.indexOf(".")+3);  
  alert(result);  
  </script>  
 ⊕本文来自: 爱问CPU学习网(http://www.iwcpu.com/) 详细出处参考:http://www.iwcpu.com/html/otherbc790.html

分类:

技术点:

相关文章:

  • 2021-10-19
  • 2021-11-02
  • 2018-05-29
  • 2021-11-18
  • 2021-03-30
  • 2021-10-19
猜你喜欢
  • 2021-10-19
  • 2021-12-23
  • 2021-09-20
  • 2021-11-18
  • 2021-11-14
  • 2021-11-18
相关资源
相似解决方案