//小计  
  function sumMoney(){
       var bol=true;
       $("#tby2>tr").each(function (i,obj){
          
           var adUnitprice=$(obj).find("td:eq(4)>input").val();  //单价(元)
           var adCount=$(obj).find("td:eq(5)>input").val();  //数量(册)
            if(adUnitprice==""||isNaN(adUnitprice)){
   $(obj).find("td:eq(4)>input").val("");
   bol=false;
}
if(adCount==""||isNaN(adCount)){
   $(obj).find("td:eq(5)>input").val("");
   bol=false;
}
if(bol==true){
  $(obj).find("td:eq(6)>input").val(parseFloat(adUnitprice*adCount));
}
            
           });
           if(bol==true){
             sumMoney2();
           }
           
  }
(parseFloat(adUnitprice*adCount))  假如:adUnitprice=0.8adCount=3
这里得出来的值 有的时候是这样的:2.4000000000000003
我只想得到2.4应该怎么做呢?

解决方案 »

  1.   


    var value = parseFloat(adUnitprice*adCount);
    alert(value.toFixed(1));
    但是js不适合浮点运算,最好在java里用BigDecimal来计算。
      

  2.   

    <script>
    function  ForDight1(Dight,How)  {
     Dight  =(Dight*Math.pow(10,How)/Math.pow(10,How)).toFixed(How);  
     return  Dight;  
    }  
    alert(ForDight1(2.4000000000000003,2));   
    </script> 
    网上的 没试过
      

  3.   

    function accMul(arg1,arg2) 

    var m=0,s1=arg1.toString(),s2=arg2.toString(); 
    try{m+=s1.split(".")[1].length}catch(e){} 
    try{m+=s2.split(".")[1].length}catch(e){} 
    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m) 

    accMul(0.8,3);
    Lz可以试试
      

  4.   

    可以先判断结果是否包含".",如果包含,则用substr取子串,小数点后保留一位即可。
      

  5.   

    hiahia     
     3.44444 | 0 = 3