feat: 新增 money.ts 工具文件,提供基于 decimal.js 的精确金额计算和格式化功能。
This commit is contained in:
@@ -75,16 +75,26 @@ export const Money = {
|
||||
},
|
||||
|
||||
/**
|
||||
* Format money for display with currency symbol
|
||||
* Format money for display with currency symbol (Standard v7.0)
|
||||
*/
|
||||
format(value: number | string, currency: string = 'CNY'): string {
|
||||
const num = new Decimal(value).toDecimalPlaces(2).toNumber();
|
||||
const symbol = CURRENCY_SYMBOLS[currency] || currency;
|
||||
const formatted = num.toLocaleString('zh-CN', {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
});
|
||||
return `${symbol}${formatted}`;
|
||||
const num = new Decimal(value).toNumber();
|
||||
try {
|
||||
return new Intl.NumberFormat('zh-CN', {
|
||||
style: 'currency',
|
||||
currency: currency,
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
}).format(num);
|
||||
} catch (e) {
|
||||
// Fallback for invalid currency codes
|
||||
const symbol = CURRENCY_SYMBOLS[currency] || currency;
|
||||
const formatted = num.toLocaleString('zh-CN', {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
});
|
||||
return `${symbol}${formatted}`;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user