js- solve the precision problem of multiplying decimals

  // 把小数转成整数,同时返回要除以的10的倍数
        function integer(num){
            // 转成字符串
            var newnum="";
            newnum=num+"";
            if(newnum.indexOf(".")==-1){
                return {int:parseInt(num),divisor:1}
            }
            // 分割字符串
            var len=newnum.split(".")[1].length;
            // 求10的倍数
            var total=1;
            while(len!=0){
                total=total*10;
                len--
            }
            // 返回整数和10的倍数
            return {int:parseInt(total*num),divisor:total}
        }

Example: 0.55 * 50000 = 27500.000000000004

var num = integer (0.55);

num.int * 50000 / num.divisor = 27500

Guess you like

Origin blog.csdn.net/liuhao9999/article/details/110466724