JS加減乘除位移方法封裝

wh_xmy發表於2018-07-11
常用加減乘除等方法彙總

直接上程式碼,邏輯簡單,程式碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math Methods</title>
</head>
<body>
    <div id="div">加減乘除位移方法彙總</div>
<script type="application/javascript">
    /**
     * 向右移動小數點 ,相當於乘10的_pointcount次冪
     * @param _number 操作的數字
     * @param _pointcount 向右移動的位數
     * @returns 移動以後的數字(number型別)
     */
    var moveRight = function (_number, _pointcount) {
        if (Number(_number) !== Number(_number)) {
            return _number;
        }
        var flag = 1;
        if (_number < 0) {
            flag = -1;
            _number = _number * -1;
        }
        var numStr = _number.toString();
        var strArr = numStr.split(".");
        var rightStr = strArr.length > 1 ? strArr[1] : "";
        //在不足移動的位數後補0;
        if (rightStr.length < _pointcount) {
            var addZero = [];
            for (var i = rightStr.length; i < _pointcount; i++) {
                addZero.push("0");
            }
            rightStr = rightStr + addZero.join("");
        }
        return Number(strArr[0] + rightStr.substring(0, _pointcount) + "." + rightStr.substr(_pointcount)) * flag;
    };
    /**
     * 向左移動小數點,相當於_number除以10的_pointcount次冪
     * @param _number 操作的數字
     * @param _pointcount 向左移動的位數
     * @returns 移動以後的數字(number型別)
     */
    var moveLeft = function (_number, _pointcount) {
        //判斷是否為數字,非數字直接返回本身
        if (Number(_number) !== Number(_number)) {
            return _number;
        }
        var flag = 1;
        if (_number < 0) {
            flag = -1;
            _number = _number * -1;
        }
        //number轉換為string ,方便後續操作。
        var numStr = _number.toString();
        //按照小數點分隔字串,
        var strArr = numStr.split(".");
        var num = 0, zeroArr = [];
        //生成移動位數+1的0 ,補到分隔後的第一個字串中,相容0.XXX的情況。開頭多餘的零會在呼叫Number方法時去掉。
        while (num < _pointcount + 1) {
            zeroArr.push("0");
            num++;
        }
        var leftStr = zeroArr.join("") + strArr[0];
        var leftArr = leftStr.split("")
        //插入移動後的小數點。
        leftArr.splice((leftStr.length - _pointcount), 0, '.');
        var result = leftArr.join("");
        //拼接原始值小數點後的數字
        if (strArr.length > 1) {
            result += strArr[1];
        }
        return Number(result) * flag;
    };
    /**
     * 加法
     * @param _num1
     * @param _num2
     * @param precision 精度
     * @returns {*}
     */
    var add = function (_num1, _num2, precision) {
        if (!precision) {
            precision = 2;
        }
        return moveLeft(moveRight(_num1, precision) + moveRight(_num2, precision), precision)
    };
    /**
     * 減法
     * @param minuend
     * @param reduction
     * @returns {*}
     */
    var reduce = function (minuend, reduction, precision) {
        if (!precision) {
            precision = 2;
        }
        return moveLeft(moveRight(minuend, precision) - moveRight(reduction, precision), precision)
    };
    /**
     * 乘法
     * @param _num1
     * @param _num2
     * @returns {number}
     */
    var multiplication = function (_num1, _num2, precision) {
        if (!precision) {
            precision = 2;
        }
        return moveLeft(moveRight(_num1, precision) * moveRight(_num2, precision), precision * 2)
    };
    /**
     * 除法
     * @param _num1 被除數
     * @param _num2 除數
     * @param precision 精度 整形,幾位小數
     * @returns {number}
     */
    var division = function (_num1, _num2, precision) {
        if (!precision) {
            precision = 2;
        }
        return moveRight(_num1, precision) / moveRight(_num2, precision);
    };
    console.log(moveRight(0.9999999,3));
    console.log(moveLeft(88888888888,2));
    console.log(add(1.01,7.079,8));
    console.log(reduce(8.01,1,2));
    console.log(multiplication(1,8.88,2));
    console.log(division(16,2,2))
</script>
</body>
</html>

相關文章