同步6/16

This commit is contained in:
Zy
2026-06-01 18:12:58 +08:00
parent 38274ab612
commit 50e7b14fd6
183 changed files with 8956 additions and 4803 deletions

View File

@@ -1,3 +1,5 @@
import Big from 'big.js';
/**
* 数字金额转中文大写(支持整数、小数)
* @param num - 待转换的金额(支持数字或字符串格式)
@@ -80,3 +82,96 @@ export function convertToChineseCapital(num: number | string): string {
return result;
}
/**
* 金额加法
* @param a 数值
* @param b 数值
* @returns 保留2位小数字符串
*/
export function moneyAdd(a: number | string = 0, b: number | string = 0): string {
return new Big(a).plus(b).toFixed(2);
}
// 金额减法
export function sub(a, b) {
let r1, r2;
try {
r1 = a.toString().split('.')[1].length;
} catch (e) {
r1 = 0;
}
try {
r2 = b.toString().split('.')[1].length;
} catch (e) {
r2 = 0;
}
const m = Math.pow(10, Math.max(r1, r2));
return (a * m - b * m) / m;
}
// 金额乘法
export function mul(a, b) {
let m = 0;
const s1 = a.toString(),
s2 = b.toString();
try {
m += s1.split('.')[1].length;
} catch (e) {}
try {
m += s2.split('.')[1].length;
} catch (e) {}
return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);
}
// 金额除法
export function div(a, b) {
let t1 = 0,
t2 = 0;
try {
t1 = a.toString().split('.')[1].length;
} catch (e) {}
try {
t2 = b.toString().split('.')[1].length;
} catch (e) {}
const x = Number(a.toString().replace('.', ''));
const y = Number(b.toString().replace('.', ''));
return (x / y) * Math.pow(10, t2 - t1);
}
// 保留2位小数金额专用
export function toFixed2(val) {
return Number(val).toFixed(2);
}
/**
* 智能金额格式化(带单位:元/万元/亿元)
* 兼容数字、字符串、null、undefined
* @param {number|string} amount 金额
* @returns {string} 格式化后带单位的金额
*/
export function formatMoneyWithUnit(amount) {
// 空值处理
if (amount == null || amount === '') return '0.00 元';
const num = Number(amount);
if (isNaN(num)) return '0.00 元';
// 绝对值判断单位
const absNum = Math.abs(num);
if (absNum >= 100000000) {
// 亿元
return (num / 100000000).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',') + ' 亿元';
} else if (absNum >= 10000) {
// 万元
return (num / 10000).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',') + ' 万元';
} else {
// 元
return num.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',') + ' 元';
}
}
// 带货币符号(¥)版本
export function formatMoneyWithSymbol(amount) {
return '¥' + formatMoneyWithUnit(amount);
}