UNPKG

@df8080/vue2-ui

Version:

🎨 一个基于 Vue 2 的 UI 组件库,目前主要面向微信小程序开发场景,也适用于其他移动端项目。

125 lines (112 loc) 3.67 kB
/** 得到小数点后数字长度 */ const digitLength = (num) => { if (!num) { return 0 } const eSplit = num.toString().split(/[eE]/) const len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0) return len > 0 ? len : 0 } /** 把错误的数据转正 strip(0.09999999999999998)=0.1 */ export const strip = (num, precision = 16) => { return +parseFloat(num.toPrecision(precision)) } /** 把小数转成整数,支持科学计数法。如果是小数则放大成整数 */ export function float2Fixed(num) { if (!num) { return 0 } if (num.toString().indexOf('e') === -1) { return Number(num.toString().replace('.', '')) } const dLen = digitLength(num) return dLen > 0 ? strip(num * Math.pow(10, dLen)) : num } /** 精确加法 */ export const plus = (...args) => { const [num1, num2, ...others] = args if (others.length > 0) { return plus(plus(num1, num2), others[0], ...others.slice(1)) } const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2))) return (times(num1, baseNum) + times(num2, baseNum)) / baseNum } /** 精确减法 */ export const minus = (num1, num2, ...others) => { if (others.length > 0) { return minus(minus(num1, num2), others[0], ...others.slice(1)) } const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2))) return (times(num1, baseNum) - times(num2, baseNum)) / baseNum } /** 精确乘法 */ export const times = (num1, num2, ...others) => { if (others.length > 0) { return times(times(num1, num2), others[0], ...others.slice(1)) } const num1Changed = float2Fixed(num1) const num2Changed = float2Fixed(num2) const baseNum = digitLength(num1) + digitLength(num2) const leftValue = num1Changed * num2Changed return leftValue / Math.pow(10, baseNum) } /** 精确除法 */ export const divide = (num1, num2, ...others) => { if (num2 === 0) { return 0 } if (others.length > 0) { return divide(divide(num1, num2), others[0], ...others.slice(1)) } const num1Changed = float2Fixed(num1) const num2Changed = float2Fixed(num2) return times(num1Changed / num2Changed, Math.pow(10, digitLength(num2) - digitLength(num1))) } /** * 四舍五入,自动补0 * @param num 待处理数字 * @param decimals 需要保留的小数位 * @return 处理后的数字 */ export const formatRound = (num, decimals = 2) => { if (isNaN(num)) { return 0 } const p1 = Math.pow(10, decimals + 1) const p2 = Math.pow(10, decimals) const absNum = Math.round((Math.abs(num) * p1) / 10) / p2 let copyNumStr = absNum.toString() if (decimals) { if (copyNumStr.indexOf('.') > 0) { const _float = copyNumStr.split('.')[1] if (_float.length < decimals) { copyNumStr = absNum + new Array(decimals - _float.length).fill(0).join('') } } else { copyNumStr = absNum + `.${new Array(decimals).fill(0).join('')}` } } return num > 0 ? Number(copyNumStr) : -Number(copyNumStr) } /** * 向下取整保留x位小数 * @param num 待处理数字 * @param decimals 需要保留的小数位 * @return 处理后的数字 */ export const floorKeepDecimals = (num, decimals = 2) => { const factor = Math.pow(10, decimals) const result = Math.floor(num * factor) / factor return Number(result.toFixed(decimals)) } /** * 向上取整保留x位小数 * @param num 待处理数字 * @param decimals 需要保留的小数位 * @return 处理后的数字 */ export const ceilKeepDecimals = (num, decimals = 2) => { const factor = Math.pow(10, decimals) const result = Math.ceil(num * factor) / factor return Number(result.toFixed(decimals)) }