Javascript Multiplication Bug

When i run this script
<script language="javascript">
alert(34.97 * 100)
alert(34.98 * 100)
alert(34.99 * 100)
alert(1.98 * 100)
alert(1.99 * 100)
</script>
I get the following results
3497
3497.9999999999995
3499
198
199

可以用toFixed(number)的方法,或者用下面修改的乘法函数

    function mul(n1, n2) {
        var m=0, s1=n1.toString(), s2=n2.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)
    }


posted @ 2011-04-29 10:49  李传涛  阅读(185)  评论(0编辑  收藏  举报