1. 新增 utils/currency.js 货币工具函数 2. 首页今日盈亏使用 todayProfitCurrency 动态获取货币符号 3. 详情页今日盈亏使用 todayProfitCurrency 而非 currency
58 lines
1.6 KiB
JavaScript
58 lines
1.6 KiB
JavaScript
/**
|
|
* 货币工具函数
|
|
*/
|
|
|
|
/**
|
|
* 获取货币符号
|
|
* @param {string} currency - 货币代码 (CNY, USD, HKD 等)
|
|
* @returns {string} 货币符号
|
|
*/
|
|
export const getCurrencySymbol = (currency) => {
|
|
const symbols = {
|
|
'CNY': '¥',
|
|
'USD': '$',
|
|
'HKD': 'HK$',
|
|
'EUR': '€',
|
|
'GBP': '£',
|
|
'JPY': '¥',
|
|
'SGD': 'S$',
|
|
'AUD': 'A$'
|
|
};
|
|
return symbols[currency?.toUpperCase()] || '¥';
|
|
};
|
|
|
|
/**
|
|
* 格式化金额
|
|
* @param {number} amount - 金额
|
|
* @param {string} currency - 货币代码
|
|
* @param {number} decimals - 小数位数
|
|
* @returns {string} 格式化后的金额字符串
|
|
*/
|
|
export const formatAmount = (amount, currency = 'CNY', decimals = 2) => {
|
|
const symbol = getCurrencySymbol(currency);
|
|
const formatted = Math.abs(amount || 0).toLocaleString('zh-CN', {
|
|
minimumFractionDigits: decimals,
|
|
maximumFractionDigits: decimals
|
|
});
|
|
const sign = amount >= 0 ? '' : '-';
|
|
return `${sign}${symbol}${formatted}`;
|
|
};
|
|
|
|
/**
|
|
* 格式化金额(带正负号)
|
|
* @param {number} amount - 金额
|
|
* @param {string} currency - 货币代码
|
|
* @param {number} decimals - 小数位数
|
|
* @returns {string} 格式化后的金额字符串
|
|
*/
|
|
export const formatAmountWithSign = (amount, currency = 'CNY', decimals = 2) => {
|
|
const symbol = getCurrencySymbol(currency);
|
|
const formatted = Math.abs(amount || 0).toLocaleString('zh-CN', {
|
|
minimumFractionDigits: decimals,
|
|
maximumFractionDigits: decimals
|
|
});
|
|
if (amount > 0) return `+${symbol}${formatted}`;
|
|
if (amount < 0) return `-${symbol}${formatted}`;
|
|
return `${symbol}${formatted}`;
|
|
};
|