zoukankan      html  css  js  c++  java
  • js浮点数的加减乘除

    ;(function(root, factory) {
    
        // Support AMD
        if (typeof define === 'function' && define.amd) {
            define([], factory);
    
            // Support CommonJS
        } else if (typeof exports === 'object') {
            var accFloating = factory();
    
            // Support NodeJS & Component, which allow module.exports to be a function
            if (typeof module === 'object' && module && module.exports) {
                exports = module.exports = accFloating;
            }
    
            // Support CommonJS 1.1.1 spec
            exports.accFloating = accFloating;
    
            // Support vanilla script loading
        } else {
            root.accFloating = factory();
        }
    
    }(this, function() {
        /**
         * 获取小数点后数字长度
         * @author zhongjiewang
         * @param  {Number} num 数字
         * @return {Number}     长度
         */
        function decimalLength(num) {
            var str = num.toString();
            var index = str.indexOf('.');
            return index == -1 ? 0 : str.substr(index + 1).length;
        }
    
        /**
         * 小数点后补齐0作为整数
         * @author zhongjiewang
         * @param  {Number} num    数字
         * @param  {Number} length 补齐的长度
         * @return {Number}        整数
         */
        function suffixInteger(num, length) {
            var str = num.toString();
            var decimalLen = decimalLength(num);
            str += Math.pow(10, length - decimalLen).toString().substr(1);
            return Number(str.replace('.', ''));
        }
    
        // 加法
        function accAdd(num1, num2) {
            var r1 = decimalLength(num1);
            var r2 = decimalLength(num2);
    
            var max = Math.max(r1, r2);
    
            var n1 = suffixInteger(num1, max);
            var n2 = suffixInteger(num2, max);
    
            return Number(((n1 + n2) / Math.pow(10, max)).toFixed(max));
        }
    
        // 减法
        function accSubtr(num1, num2) {
            var r1 = decimalLength(num1);
            var r2 = decimalLength(num2);
    
            var max = Math.max(r1, r2);
    
            var n1 = suffixInteger(num1, max);
            var n2 = suffixInteger(num2, max);
    
            return Number(((n1 - n2) / Math.pow(10, max)).toFixed(max));
        }
        // 乘法
        function accMul(num1, num2) {
            var r1 = decimalLength(num1);
            var r2 = decimalLength(num2);
    
            var max = Math.max(r1, r2);
    
            var n1 = suffixInteger(num1, max);
            var n2 = suffixInteger(num2, max);
    
            return n1 * n2 / Math.pow(10, max * 2);
    
        }
        // 除法
        function accDiv(num1, num2) {
            var r1 = decimalLength(num1);
            var r2 = decimalLength(num2);
    
            var max = Math.max(r1, r2);
    
            var n1 = suffixInteger(num1, max);
            var n2 = suffixInteger(num2, max);
    
            return n1 / n2;
        }
    
        return {
            add: accAdd,
            subtr: accSubtr,
            mul: accMul,
            div: accDiv
        }
    
    }));
    

      

  • 相关阅读:
    bzoj 3155: Preprefix sum
    bzoj 1854: [Scoi2010]游戏
    UVA1608 不无聊的序列 Non-boring sequences
    UVA1747 【Swap Space】
    Luogu P5550 Chino的数列
    bzoj 1799: [Ahoi2009]self 同类分布
    bzoj 1054: [HAOI2008]移动玩具
    MATLAB工具箱,应用程序,软件和资源的精选清单
    论文格式排版Issue及解决办法
    《将博客搬至CSDN》
  • 原文地址:https://www.cnblogs.com/love314159/p/10120418.html
Copyright © 2011-2022 走看看