/** * @Description: 千分位转换 * @author cr * @param {Number} value 金额 * @param {Number} decimal 要保留的小数位,默认两位 * @date 2020/4/18 * @time 12:05 */ export function formatMoney(value: any, decimal: any) { const _decimal = decimal || 2; let number = toDecimal(value, _decimal); number = executeLcommafy(number); if (value === '' || (value == null && value !== 0)) { return ''; } return number; } /** * 将金额转为万元(一万)(小数) * @param amount String 金额 */ export function toMillion(amount: any) { amount = amount || 0; amount = (parseFloat(amount) / 10000).toFixed(2); var chararr = []; //用来存储金额字符串中的每一个字符 var rlength = amount.indexOf(".") > 0 ? amount.indexOf(".") : amount.length; //如果有小数点,则只考虑小数点之前的数字进行处理 for (let i = 0; i < amount.length; i++) { if ((rlength - i > 0) && ((rlength - i) % 3 == 0) && (i != 0)) { //长度减索引表示实际要处理字符串倒序的序号,如果能整除3则在前加逗号 chararr.push(','); } chararr.push(amount.charAt(i)); } return chararr.join(''); //join方法重新将字符串数组拼接成字符串 } /** * @Description: 加千分位 * @author cr * @date 2020/4/18 * @time 12:12 */ function executeLcommafy(val: any) { if (!val || !isNumber(val)) { return ''; } if (isNumber(val)) { val = val.toString(); } if (/^.*\..*$/.test(val)) { const pointIndex = val.lastIndexOf('.'); let intPart = val.substring(0, pointIndex); const pointPart = val.substring(pointIndex + 1, val.length); intPart = intPart + ''; let re = /(-?\d+)(\d{3})/; while (re.test(intPart)) { intPart = intPart.replace(re, '$1,$2'); } val = intPart + '.' + pointPart; } else { var re = /(-?\d+)(\d{3})/; while (re.test(val)) { val = val.replace(re, '$1,$2'); } } return val; } /** * @Description: 保留小数位数 * @author cr * @date 2020/4/18 * @time 12:01 */ function toDecimal(value: any, decimal: any) { if (isNumber(value)) { if (isNumber(decimal)) { return roundFixed(value, decimal); } else { return value; } } else { return null; } } /** * @Description: 检测是否为数字 * @author cr * @date 2020/4/18 * @time 12:01 */ function isNumber(val: any) { let regPos = /^\d+(\.\d+)?$/; //非负浮点数 let regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数 return regPos.test(val) || regNeg.test(val); } /** * @Description: 四舍五入 * @author cr * @date 2020/4/18 * @time 12:02 */ function roundFixed(num: any, fixed: any) { let pos = num.toString().indexOf('.'); let decimal_places = num.toString().length - pos - 1; let _int = num * Math.pow(10, decimal_places); let divisor_1 = Math.pow(10, decimal_places - fixed); let divisor_2 = Math.pow(10, fixed); const value = Math.round(_int / divisor_1) / divisor_2; return prefixZero(value, fixed); } /** * @Description: 精度 * @author cr * @date 2020/4/18 * @time 12:03 */ function prefixZero(value: any, num: any) { let a, b, c, i; a = value.toString(); b = a.indexOf('.'); c = a.length; if (num === 0) { if (b !== -1) { a = a.substring(0, b); } } else { /*如果没有小数点*/ if (b === -1) { a = a + '.'; for (i = 1; i <= num; i++) { a = a + '0'; } } else { /*有小数点,超出位数自动截取,否则补0*/ a = a.substring(0, b + num + 1); for (i = c; i <= b + num; i++) { a = a + '0'; } } } return a; }