haw-utils
Version:
一个基于业务场景的工具方法库
57 lines (56 loc) • 1.75 kB
JavaScript
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports", "../utils/math.util"], factory);
} else if (typeof exports !== "undefined") {
factory(exports, require("../utils/math.util"));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.utilsMathUtil);
global.times = mod.exports;
}
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _math) {
"use strict";
Object.defineProperty(_exports, "__esModule", {
value: true
});
_exports["default"] = void 0;
/**
* 精确乘法,支持多个数相乘
*
* @static
* @alias module:Math.times
* @since 3.1.0
* @param {Number} num1 相乘的第一个数
* @param {Number} num2 相乘的第二个数
* @param {Number} [others] 相乘的其余数
* @returns {Number} 乘积
* @example
*
* times(3, 0.6);
* // => 1.8
*
* times(3, 0.6, 2);
* // => 3.6
*
* times(3, 0.6, 2, 10);
* // => 36
*/
function times(num1, num2) {
for (var _len = arguments.length, others = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
others[_key - 2] = arguments[_key];
}
if (others.length > 0) {
return times.apply(void 0, [times(num1, num2)].concat(others));
}
var num1Changed = (0, _math.float2Fixed)(num1);
var num2Changed = (0, _math.float2Fixed)(num2);
var baseNum = (0, _math.digitLength)(num1) + (0, _math.digitLength)(num2);
var leftValue = num1Changed * num2Changed;
(0, _math.checkBoundary)(leftValue);
return leftValue / Math.pow(10, baseNum);
}
var _default = times;
_exports["default"] = _default;
});