<script type="text/javascript">
 function round(a,b)
{
 debugger;
 var g =(Math.round(a*Math.pow(10,b))*Math.pow(10,-b));
 return g ;
 }
alert(round(0.94,2));
alert(round(0.93,2));
alert(round(0.95,2));
alert(round(0.96,2));
</script>94*0.01=0.9400000000000001
93*0.01=0.93
94*0.1=9.4
94*0.001=0.094
这到底是为什么,谁能回答一下

解决方案 »

  1.   

    直接除吧,至少能凑合用!L@_@K
    function round(a,b)
    {
        debugger;
        var g =(Math.round(a*Math.pow(10,b))/Math.pow(10,b));
        return g ;
    }
    function showInPage(output) {
        document.write(output + "<br />");
    }
    showInPage(round(0.94,2));
    showInPage(round(0.93,2));
    showInPage(round(0.95,2));
    showInPage(round(0.96,2));
    showInPage("<hr />");
      

  2.   

    楼上的方法只是为了显示,更彻底的应该是重新写
    Math.round
    Math.pow
    两个方法.
      

  3.   

    不可能滴,这是由于 JS 内置的浮点运算精度较低所导致滴!除非重写 JS 引擎!!!