utils.js
加减乘除运算
/** * @description: 加法运算 * @param {*} arg1 * @param {*} arg2 * @param {*} number 展示小数点后位数 * @return {*} */ export function operationAdd(arg1, arg2, number = 2) { let l1 = 0, l2 = 0, m, c; try { l1 = arg1.toString().split(".")[1].length; } catch (e) {} try { l2 = arg2.toString().split(".")[1].length; } catch (e) {} c = Math.abs(l1 - l2); m = Math.pow(10, Math.max(l1, l2)); if (c > 0) { let cm = Math.pow(10, c); if (l1 > l2) { arg1 = Number(arg1.toString().replace(".", "")); arg2 = Number(arg2.toString().replace(".", "")) * cm; } else { arg1 = Number(arg1.toString().replace(".", "")) * cm; arg2 = Number(arg2.toString().replace(".", "")); } } else { arg1 = Number(arg1.toString().replace(".", "")); arg2 = Number(arg2.toString().replace(".", "")); } const sum = (arg1 + arg2) / m; let max = 0; try { max = sum.toString().split(".")[1].length; } catch (error) {} return sum.toFixed(number); // return max > number ? sum.toFixed(number) : sum; } /** * @description: 减法运算 * @param {*} arg1 * @param {*} arg2 * @param {*} number 展示小数点后位数 * @return {*} */ export function operationSub(arg1, arg2, number = 2) { let l1 = 0, l2 = 0, m, n; try { l1 = arg1.toString().split(".")[1].length; } catch (e) { l1 = 0; } try { l2 = arg2.toString().split(".")[1].length; } catch (e) { l2 = 0; } m = Math.pow(10, Math.max(l1, l2)); //last modify by deeka //动态控制精度长度 n = l1 >= l2 ? l1 : l2; const sum = (arg1 * m - arg2 * m) / m; let max = 0; try { max = sum.toString().split(".")[1].length; } catch (error) {} return sum.toFixed(number); // return max > number ? sum.toFixed(number) : sum.toFixed(n); } /** * @description: 乘法运算 * @param {*} arg1 * @param {*} arg2 * @param {*} number 展示小数点后位数 * @return {*} */ export function operationMul(arg1, arg2, number = 2) { let m = 0, l1 = 0, l2 = 0, s1 = arg1.toString(), s2 = arg2.toString(); try { m += s1.split(".")[1].length; l1 = arg1.toString().split(".")[1].length; } catch (e) { l1 = 0; } try { m += s2.split(".")[1].length; l2 = arg2.toString().split(".")[1].length; } catch (e) { l2 = 0; } const sum = (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) / Math.pow(10, m); let max = 0; try { max = sum.toString().split(".")[1].length; } catch (error) {} return sum.toFixed(number); // return max > number ? sum.toFixed(number) : sum; } /** * @description: 除法运算 * @param {*} arg1 * @param {*} arg2 * @param {*} number 展示小数点后位数 * @return {*} */ export function operationDiv(arg1, arg2, number = 2) { let l1 = 0, l2 = 0, r1, r2; try { l1 = arg1.toString().split(".")[1].length; } catch (e) {} try { l2 = arg2.toString().split(".")[1].length; } catch (e) {} r1 = Number(arg1.toString().replace(".", "")); r2 = Number(arg2.toString().replace(".", "")); const sum = (r1 / r2) * Math.pow(10, l2 - l1); let max = 0; try { max = sum.toString().split(".")[1].length; } catch (error) {} return sum.toFixed(number); // return max > number ? sum.toFixed(number) : sum; }
数字添加千分位分隔符
/** * 添加千分位 * @param {Object} value */ export const formatThousands = (value) => { if (value === undefined || value === null || value == "0") { value = "0.00"; } if (!isNaN(value)) { value = value + ""; } let re = /\d{1,3}(?=(\d{3})+$)/g; let n1 = value.replace(/^(\d+)((\.\d+)?)$/, function (s, s1, s2) { return s1.replace(re, "$&,") + s2; }); return n1; };
获取几个月前的日期
// 获取几个月前的日期 export function getRecentMonth(monthNum = 0) { let date = new Date(); date.setMonth(date.getMonth() - monthNum, 1); return { year: date.getFullYear(), month: date.getMonth() + 1, }; }