阿里云折扣码

轻云博客 > Jquery+Ajax学习 > jquery处理数字保留2位小数,强制保留2位小数不够补上.00

jquery处理数字保留2位小数,强制保留2位小数不够补上.00

作者:Aisencici / 日期:2017-2-9 9:45:00 / 分类:Jquery+Ajax学习 / 浏览:4155

1、保留两位小数    //功能:将浮点数四舍五入,取小数点后2位

2、//制保留2位小数,如:2,会在2后面补上00.即2.00  

  1. <script type="text/javascript">    

  2.     //保留两位小数     

  3.     //功能:将浮点数四舍五入,取小数点后2位    

  4.     function toDecimal(x) {    

  5.         var f = parseFloat(x);    

  6.         if (isNaN(f)) {    

  7.             return;    

  8.         }    

  9.         f = Math.round(x*100)/100;    

  10.         return f;    

  11.     }    

  12.   

  13.   

  14.     //制保留2位小数,如:2,会在2后面补上00.即2.00    

  15.     function toDecimal2(x) {    

  16.         var f = parseFloat(x);    

  17.         if (isNaN(f)) {    

  18.             return false;    

  19.         }    

  20.         var f = Math.round(x*100)/100;    

  21.         var s = f.toString();    

  22.         var rs = s.indexOf('.');    

  23.         if (rs < 0) {    

  24.             rs = s.length;    

  25.             s += '.';    

  26.         }    

  27.         while (s.length <= rs + 2) {    

  28.             s += '0';    

  29.         }    

  30.         return s;    

  31.     }    

  32.         

  33.     function fomatFloat(src,pos){       

  34.          return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);       

  35.     }    

  36.       

  37.     document.write("四舍五入  <br/>")  

  38.     document.write("3.14159267保留2位小数:" + toDecimal(3.14159267)+"<br/>");    

  39.     document.write("3.14159267强制保留2位小数:" + toDecimal2(3.14159267)+"<br/>");    

  40.     document.write("3.14159267保留2位小数:" + toDecimal(3.14559267)+"<br/>");    

  41.     document.write("3.14159267强制保留2位小数:" + toDecimal2(3.15159267)+"<br/>");    

  42.     document.write("3.14159267保留2位小数:" + fomatFloat(3.14559267, 2)+"<br/>");    

  43.     document.write("3.14159267保留1位小数:" + fomatFloat(3.15159267, 1)+"<br/>");    

  44.         

  45.     document.write("五舍六入  <br/>")  

  46.     document.write("1000.003保留2位小数:" + 1000.003.toFixed(2)+"<br/>");    

  47.     document.write("1000.08保留1位小数:" + 1000.08.toFixed(1)+"<br/>");    

  48.     document.write("1000.04保留1位小数:" + 1000.04.toFixed(1)+"<br/>");    

  49.     document.write("1000.05保留1位小数:" + 1000.05.toFixed(1)+"<br/>");    

  50.         

  51.     document.write("科学计数  <br/>")  

  52.     document.write(3.1415+"科学技术后:"+3.1415.toExponential(2)+"<br/>");    

  53.     document.write(3.1455+"科学技术后:"+3.1455.toExponential(2)+"<br/>");    

  54.     document.write(3.1445+"科学技术后:"+3.1445.toExponential(2)+"<br/>");    

  55.     document.write(3.1465+"科学技术后:"+3.1465.toExponential(2)+"<br/>");    

  56.     document.write(3.1665+"科学技术后:"+3.1665.toExponential(1)+"<br/>");    

  57.     document.write("精确到n位,不含n位  <br/>")  

  58.     document.write("3.1415精确到小数点第2位" + 3.1415.toPrecision(2)+"<br/>");    

  59.     document.write("3.1455精确到小数点第3位" + 3.1465.toPrecision(3)+"<br/>");    

  60.     document.write("3.1445精确到小数点第2位" + 3.1415.toPrecision(2)+"<br/>");    

  61.     document.write("3.1465精确到小数点第2位" + 3.1455.toPrecision(2)+"<br/>");    

  62.     document.write("3.166592679287精确到小数点第5位" + 3.141592679287.toPrecision(5)+"<br/>");    

  63. </script>    


本文标签:
From:
分享到: