js计算误差修正代码(真实版)

/*乘法操作

        * 1.返回精确的两数相乘的结果,解决了精度的问题

        * 2.用此方法返回的结果再调用toFixed方法不会出错

        */

        function mulFuncion(num1, num2) {

            var totalDigits = 0;

            num1 = num1.toString();

            num2 = num2.toString();

            var numArr1 = num1.split('.');

            var numArr2 = num2.split('.');

            try { totalDigits += numArr1[1].length; } catch (e) { }

            try { totalDigits += numArr2[1].length; } catch (e) { }

            var number = num1.replace('.', '') * num2.replace('.', '');

            return number / Math.pow(10, totalDigits);

        }

 

        /*除法操作

        * 1. 返回精确的两数相除的结果,解决了精度的问题

        * 2. 用此方法返回的结果再调用toFixed方法不会出错

        */

        function divFuncion(num1, num2) {

            var decimal1 = 0, decimal2 = 0, decimal1, decimal2;

            try { decimal1 = num1.toString().split(".")[1].length } catch (e) { }

            try { decimal2 = num2.toString().split(".")[1].length } catch (e) { }

            with (Math) {

                decimal1 = Number(num1.toString().replace(".", ""))

                decimal2 = Number(num2.toString().replace(".", ""))

                return (decimal1 / decimal2) * pow(10, decimal2 - decimal1);

            }

        }

 

        /*加法操作

        * 1. 返回精确的两数相加的结果,解决了精度的问题

        * 2. 用此方法返回的结果再调用toFixed方法不会出错

        */

        function addFuncion(num1, num2) {

            var decimal1, decimal2, multiple;

            try { decimal1 = num1.toString().split(".")[1].length } catch (e) { decimal1 = 0 }

            try { decimal2 = num2.toString().split(".")[1].length } catch (e) { decimal2 = 0 }

            multiple = Math.pow(10, Math.max(decimal1, decimal2))

            return (num1 * multiple + num2 * multiple) / multiple;

        }

 

        /*减法操作

        * 1. 返回精确的两数相减的结果,解决了精度的问题

        * 2. 用此方法返回的结果再调用toFixed方法不会出错

        */

        function subFuncion(num1, num2) {

            var decimal1, decimal2, multiple, maxDecimal;

            try { decimal1 = num1.toString().split(".")[1].length } catch (e) { decimal1 = 0 }

            try { decimal2 = num2.toString().split(".")[1].length } catch (e) { decimal2 = 0 }

            multiple = Math.pow(10, Math.max(decimal1, decimal2));

           

            //动态控制精度长度

            maxDecimal = (decimal1 >= decimal2) ? decimal1 : decimal2;

            return ((num1 * multiple - num2 * multiple) / multiple).toFixed(maxDecimal);

        }

/*将number四舍五入到decimals位

        * 1.number可以是数字或数字字符串(计算得来的如262.2000000000003这样的数字也行),不支持科学计数法的数字

        * 2.decimals可以是数字或数字字符串(0和负数也行)

        */

        function format_number(number, decimals) {

            if (isNaN(number)) { return 0 };

            if (number == '') { return 0 };

 

            decimals = parseInt(decimals);

            number = parseFloat(number);

            alert(number);

            var numStr = new String(number);

            var numArr = numStr.split('.');

            var whole = parseFloat(numArr[0]);

            var result = '';

 

            // 如果decimals是正数

            if (decimals > 0) {

                if (numArr.length > 1) {

                    var dec = new String(numArr[1]);

                    dec = String(parseFloat(dec) / Math.pow(10, (dec.length - decimals)));

                    dec = String(whole + Math.round(parseFloat(dec)) / Math.pow(10, decimals));

                    var dot = dec.indexOf('.');

                    if (dot == -1) {

                        dec += '.';

                        dot = dec.indexOf('.');

                    }

                    var totalLength = dot + decimals;

                    while (dec.length <= totalLength) {

                        dec += '0';

                    }

                    result = dec;

                } else {

                    var dot;

                    var dec = new String(whole);

                    dec += '.';

                    dot = dec.indexOf('.');

                    while (dec.length <= dot + decimals) { dec += '0'; }

                    result = dec;

                }

            }

            // 如果decimals是负数

            else {

                decimals = 0 - decimals;

                if (numArr[0].length - decimals - 1 < 0) {

                    result = "0";

                } else {

                    var leftLength = numArr[0].toString().length - decimals;

                    whole = parseInt(numStr.substring(0, leftLength));

                    var dec = parseFloat(numStr.substr(leftLength, numStr.length));

                    dec = Math.round(dec / Math.pow(10, decimals));

                    dec = String(whole + dec);

                    while (dec.length < numArr[0].length) {

                        dec += "0";

                    }

                    result = dec;

                }

            }

            return result;

        }

原文地址:https://www.cnblogs.com/xxzkj/p/4194087.html