javascript高精度计算解决方案

来源:互联网 发布:永恒之塔捏脸数据 编辑:程序博客网 时间:2024/06/01 07:33

首先解决精度最便捷的方式:

x为要做精度处理的数值,先将x放大10000倍,再四舍五入,在除以10000倍。

Math.round(x*10000)/10000

注意:这里乘的倍数和你要保留的小数位数相对应。比如要保留2位,则乘100倍;保留3位,则乘1000倍。

该方法可以保证大部分情况下适用。


如果你要保证极其高的精度,则需要采用下面的方法处理。

--------------------------------------------------------------------------------------------------------------------------------------------

1,简介             Javascript中高精度计算,主要解决Javascript不精确问题,此解决方案包括,浮点数数计算出现不精确问题;浮点数计算中指定精度的方法。计算不精确通过 Arithmetic(函数)处理;指定精度通过FormatByAccuracy(函数)处理,这两个函数也是本实例中最主要的两个方法。可用于JS开发和.NET的ajax开发项目中。

2,实例

 

数量精度: 单价精度: 金额精度:数量单价金额

3,code

[html] view plain copy
  1.  <pre class="html" name="code"><html>  
  2.   
  3. <head>   
  4.   
  5.     <title>javascript高精度计算解决方案</title>  
  6.   
  7.     <script language="javascript" type="text/javascript">  
  8.   
  9.           
  10.   
  11.         function AutoCalculate(rowid,flag){  
  12.   
  13.             /// <summary>自动计算数量、单价、金额</summary>     
  14.   
  15.             /// <param name="obj">输入框控件</param>       
  16.   
  17.             /// <param name="rowid">new rowid</param>     
  18.   
  19.             /// <param name="flag">N数量P单价M金额</param>      
  20.   
  21.             var objN = document.getElementById("txtN"+rowid);  
  22.   
  23.             var objP = document.getElementById("txtP"+rowid);  
  24.   
  25.             var objM = document.getElementById("txtM"+rowid);  
  26.   
  27.               
  28.   
  29.             //取设置的精度  
  30.   
  31.             var np = document.getElementById("txtNP").value;  
  32.   
  33.             var pp = document.getElementById("txtPP").value;  
  34.   
  35.             var mp = document.getElementById("txtMP").value;  
  36.   
  37.               
  38.   
  39.             //check  
  40.   
  41.             if(np>21||pp>21||mp>21)  
  42.   
  43.             {  
  44.   
  45.               alert('精度最大21!');   
  46.   
  47.               return;  
  48.   
  49.             }  
  50.   
  51.               
  52.   
  53.             //数据精度处理  
  54.   
  55.             objN.value = FormatByAccuracy(objN.value,np);  
  56.   
  57.             objP.value = FormatByAccuracy(objP.value,pp);  
  58.   
  59.                   
  60.   
  61.             //进行计算  
  62.   
  63.             if(flag=="N"){ //修改数量  
  64.   
  65.                 if(objN.value!=""){ //如果数量栏位值不为空  
  66.   
  67.                     if(objP.value != ""){ // 如果单价不为空,则重新计算金额  
  68.   
  69.                         objM.value = Arithmetic(objN.value,'*',objP.value);  
  70.   
  71.                     }else{   
  72.   
  73.                         if(objM.value!=""){ //如果单价为空,金额不为空,计算出单价  
  74.   
  75.                             if(objN.value!=0){//如果数量不为0  
  76.   
  77.                                 objP.value = Arithmetic(objM.value,'/',objN.value);  
  78.   
  79.                             }  
  80.   
  81.                         }  
  82.   
  83.                     }  
  84.   
  85.                 }else{  //如果数量栏位值为空,则清空金额栏位。  
  86.   
  87.                     objM.value = "";  
  88.   
  89.                 }  
  90.   
  91.             }else if(flag=="P"){//修改单价  
  92.   
  93.                  if(objP.value!=""){ //如果单价栏位值不为空  
  94.   
  95.                       if(objN.value!=""){ //如果数量栏位值不为空,重算金额栏位  
  96.   
  97.                           objM.value = Arithmetic(objN.value,'*',objP.value);  
  98.   
  99.                       }else{//数量栏位值为空,清空金额栏位  
  100.   
  101.                           objM.value = "";  
  102.   
  103.                       }  
  104.   
  105.                  }else{//如果单价栏位值为空  
  106.   
  107.                       if(objN.value!=""){ //如果数量栏位值不为空,清空金额栏位  
  108.   
  109.                           objM.value = "";  
  110.   
  111.                       }else{  
  112.   
  113.                         
  114.   
  115.                       }  
  116.   
  117.                  }  
  118.   
  119.             }else if(flag=="M"){//修改金额  
  120.   
  121.                  if(objM.value!=""){ //如果金额栏位值不为空  
  122.   
  123.                       if(objN.value!=""){ //如果数量栏位值不为空,重算单价栏位  
  124.   
  125.                           if(objN.value!="0"){//如果数量不为0  
  126.   
  127.                               objP.value = Arithmetic(objM.value,'/',objN.value);  
  128.   
  129.                           }  
  130.   
  131.                       }else{//数量栏位值为空,清空单价栏位  
  132.   
  133.                           objP.value = "";  
  134.   
  135.                       }  
  136.   
  137.                  }else{//如果金额栏位值为空  
  138.   
  139.                       if(objN.value!=""){ //如果数量栏位值不为空,清空单价栏位  
  140.   
  141.                           objP.value = "";  
  142.   
  143.                       }else{  
  144.   
  145.                       }  
  146.   
  147.                  }  
  148.   
  149.             }     
  150.   
  151.             //处理计算结果精度,有可能是算金额,有可能是算单价  
  152.   
  153.             objP.value = FormatByAccuracy(objP.value,pp);  
  154.   
  155.             objM.value = FormatByAccuracy(objM.value,mp);  
  156.   
  157.               
  158.   
  159.             //计算合计金额  
  160.   
  161.             CalculateSumMoney();  
  162.   
  163.         }  
  164.   
  165.           
  166.   
  167.         function CalculateSumMoney(){  
  168.   
  169.             /// <summary>计算合计金额</summary>  
  170.   
  171.             var objM = document.getElementsByName("txtM");  
  172.   
  173.             var sumMoney =0;  
  174.   
  175.             var m;  
  176.   
  177.             for(var i=0;i<objM.length;i++){  
  178.   
  179.                 m = objM.item(i).value;  
  180.   
  181.                 if(m != ""){  
  182.   
  183.                     sumMoney = Arithmetic(sumMoney,'+',m);  
  184.   
  185.                 }  
  186.   
  187.             }  
  188.   
  189.             document.getElementById("txtSum").value = sumMoney;  
  190.   
  191.         }  
  192.   
  193.         function FormatByAccuracy(val,accuracy){  
  194.   
  195.             /// <summary>浮点数精度处理</summary>  
  196.   
  197.             /// <par accuracy>小数位精度</par>  
  198.   
  199.             /// <bug>由于toPrecision是从第一个不为0的值开始处理精度,  
  200.   
  201.             ///所以暂不考虑0.00000X(<0.01)的情况</bug>  
  202.   
  203.             if(val){  
  204.   
  205.                 if(accuracy==0&&parseFloat(val)<1){  
  206.   
  207.                     return parseFloat(val).toPrecision();  
  208.   
  209.                 }else{  
  210.   
  211.                     val = Number(val).toString();  
  212.   
  213.                     index = val.indexOf('.');  
  214.   
  215.                     //len整数位精度  
  216.   
  217.                     len = index==-1?val.length:(val.substr(0,index)=='0'?index-1:index);  
  218.   
  219.                     accuracy = parseInt(len,10)+parseInt(accuracy,10);  
  220.   
  221.                     //toPrecision最大支持21位处理  
  222.   
  223.                     accuracy = accuracy>21?21:accuracy;                  
  224.   
  225.                     return parseFloat(val).toPrecision(accuracy);  
  226.   
  227.                 }  
  228.   
  229.             }else{  
  230.   
  231.                 return val;  
  232.   
  233.             }  
  234.   
  235.         }  
  236.   
  237.         function Arithmetic(arg1,operator,arg2){  
  238.   
  239.             ///<summary>四则运算,基本思路:转整计算然后恢复小数位</summary>  
  240.   
  241.             ///<par>operator运算符</par>  
  242.   
  243.             ///<result>计算结果</result>  
  244.   
  245.             var r1,r2,mul,size;  
  246.   
  247.             try{  
  248.   
  249.                 r1=arg1.toString().split(".")[1].length;  
  250.   
  251.             }catch(e){  
  252.   
  253.                 r1=0;  
  254.   
  255.             }  
  256.   
  257.             try{  
  258.   
  259.                 r2=arg2.toString().split(".")[1].length;  
  260.   
  261.             }catch(e){  
  262.   
  263.                 r2=0;  
  264.   
  265.             }  
  266.   
  267.             size = Math.max(r1,r2);  
  268.   
  269.             switch(operator){  
  270.   
  271.                     case "+":  
  272.   
  273.                     case "-":  
  274.   
  275.                             mul = size;  
  276.   
  277.                             break;  
  278.   
  279.                     case "*":  
  280.   
  281.                             mul = 2 * size;  
  282.   
  283.                             break;  
  284.   
  285.                     case "/":  
  286.   
  287.                             mul = 0;  
  288.   
  289.                             break;  
  290.   
  291.             }  
  292.   
  293.             return eval((arg1*Math.pow(10, size)) + operator + (arg2*Math.pow(10, size))) / Math.pow(10, mul);  
  294.   
  295.         }  
  296.   
  297.   
  298.   
  299.     </script>  
  300.   
  301. </head>  
  302.   
  303. <body>  
  304.   
  305.     <form id="form1" >  
  306.   
  307.         <h3>简介</h3>  
  308.   
  309.         Javascript中高精度计算,主要解决Javascript不精确问题,此解决方案包括,浮点数数计算出现不精确问题;浮点数计算中指定精度的方法。<br>计算不精确通过 Arithmetic(函数)处理;指定精度通过FormatByAccuracy(函数)处理,这两个函数也是本实例中最主要的两个方法。<br> 可用于JS开发和.NET的ajax开发项目中。</  
  310.   
  311.         <hr>  
  312.   
  313.         数量精度:<input type="text" id="txtNP" value="1" />  
  314.   
  315.         单价精度:<input type="text" id="txtPP" value="1" />  
  316.   
  317.         金额精度:<input type="text" id="txtMP" value="1" />  
  318.   
  319.         <table width="600" border="1" cellpadding="0" id="tblMats" cellspacing="0" >  
  320.   
  321.           <tr height="20" align="center">   
  322.   
  323.             <th  width="60px">数量</th>    
  324.   
  325.             <th  width="60px">单价</th>   
  326.   
  327.             <th  width="70px">金额</th>  
  328.   
  329.           </tr>  
  330.   
  331.           <tr height="21"  align="center">      
  332.   
  333.             <td >  
  334.   
  335.                 <input width="40" type="text" id="txtN0" onblur="AutoCalculate(0,'N')" style="width:100%; text-align:right"  />  
  336.   
  337.             </td>                                  
  338.   
  339.             <td >  
  340.   
  341.                 <input width="40" type="text" id="txtP0" onblur="AutoCalculate(0,'P')" style="width:100%; text-align:right"  />  
  342.   
  343.             </td>  
  344.   
  345.             <td >  
  346.   
  347.                 <input width="40" type="text" name="txtM" id="txtM0" onblur="AutoCalculate(0,'M')" style="width:100%; text-align:right"  />  
  348.   
  349.             </td>  
  350.   
  351.           </tr>  
  352.   
  353.           <tr height="21"  align="center">       
  354.   
  355.             <td >  
  356.   
  357.                 <input width="40" type="text" id="txtN1" onblur="AutoCalculate(1,'N')" style="width:100%; text-align:right"  />  
  358.   
  359.             </td>                                  
  360.   
  361.             <td >  
  362.   
  363.                 <input width="40" type="text" id="txtP1" onblur="AutoCalculate(1,'P')" style="width:100%; text-align:right"  />  
  364.   
  365.             </td>  
  366.   
  367.             <td >  
  368.   
  369.                 <input width="40" type="text" name="txtM" id="txtM1" onblur="AutoCalculate(1,'M')" style="width:100%; text-align:right"  />  
  370.   
  371.             </td>  
  372.   
  373.           </tr>  
  374.   
  375.           <tr height="21"  align="center">  
  376.   
  377.             <td  colspan="8">  
  378.   
  379.                 <input type="text" id="txtSum" value="0"  readonly="readonly" style="width:100%;text-align:right; "  /></td>  
  380.   
  381.   
  382.   
  383.           </tr>  
  384.   
  385.     </table>  
  386.   
  387.     </form>  
  388.   
  389. </body>  
  390.   
  391. </html>  
  392.   
  393. </pre>  
原创粉丝点击