UNPKG

linking-utils

Version:

My

30 lines (29 loc) 790 B
/** * 检测当前值是否是一个对象 * @param {*} options 对象 * @returns */ export const isObject = (options) => { let type = typeof options; if (options !== null && (type === "object" || type === "function")) return true; }; /** * 小数相加计算 * @param {*} coefficient 用来计算出最大次幂 * @returns */ const coefficient = (num) => { num = num + ""; let [, char = ""] = num.split("."), len = char.length; return Math.pow(10, len); // -> 10**len }; export const plus = (num1, num2) => { num1 = +num1; num2 = +num2; // 计算的内容必须为数字类型 if (isNaN(num1) || isNaN(num2)) return NaN; let max = Math.max(coefficient(num1), coefficient(num2)); return (num1 * max + num2 * max) / max; };