<input id=text1 value=70.0555>
<input id=text2 value=7>
<script>
alert(parseInt(parseFloat(parseFloat(document.all.text1.value)*parseFloat(document.all.text2.value))*100)/100);
</script>

解决方案 »

  1.   

    ie的bug<script>
    function Number.prototype.Fixed(num){
    with(Math){
    var m=pow(10,Number(num))
    return round(this*m)/m;
    }
    }
    alert(c(56.10,23.12).Fixed(2));
    function c(n1,n2){
    var m=0,s1=n1.toString(),s2=n2.toString();
    m=s1.split(".")[1].length
    m=s2.split(".")[1].length
    return (n1-n2).Fixed(m)//高版本可以用(n1-n2).toFixed(m)
    }
    </script>
      

  2.   

    JS做浮点运算不精确可以先变为整数相乘后,再除以移位数function multi(p1,p2){
    var t1=0,t2=0,r1,r2;
    try{t1=p1.toString().split(".")[1].length}catch(e){}
    try{t2=p2.toString().split(".")[1].length}catch(e){}
    with(Math){
    r1=p1*pow(10,t1)
    r2=p2*pow(10,t2)
    return ((r1*r2)/pow(10,t2+t1));
    }
    }
    alert(multi(70.0555, 7));
      

  3.   

    你不是学计算机出身的吧?
    数值在计算机的机内表示, 并不象你用笔在纸上记数! 所以就会产生误差!
    所以,判断 2 个数是否相等, 真正的判断应当是:
    if (a-b)<0.0000001 then ……
    因为2个相等的数,在计算机里可能因为误差,被计算机认为不相等。
    这样,你该明白相除的结果,有好多位小数的原因了吧?
      

  4.   

    同意wsj(骆驼)的说法,关键是js做浮点运算不够精确。谢谢各位的指教。