Files
PolyHermes/frontend/src/utils/index.ts
T
WrBug 4d322424c1 feat: 统一USDC金额格式化,添加formatUSDC工具函数
- 新增 formatUSDC 函数:最多显示4位小数,自动去除尾随零(截断,不四舍五入)
- 统一导出所有工具函数到 utils/index.ts
- 替换所有页面中的USDC显示为 formatUSDC 函数
  - TemplateList.tsx
  - CopyTradingAdd.tsx
  - PositionList.tsx
  - AccountList.tsx
  - AccountDetail.tsx
  - OrderList.tsx
  - Statistics.tsx
- 更新前端开发规范,添加USDC格式化规范说明
- 统一工具函数导入方式(从 ../utils 导入)
2025-12-01 23:22:18 +08:00

40 lines
1.1 KiB
TypeScript

/**
* 格式化 USDC 金额
* 最多显示 4 位小数,自动去除尾随零(截断,不四舍五入)
* @param value - 金额值(字符串或数字)
* @returns 格式化后的字符串,如果值为空或无效则返回 '-'
* @example
* formatUSDC(1.23) => "1.23"
* formatUSDC(1.23456) => "1.2345"
* formatUSDC(1.2) => "1.2"
* formatUSDC(1) => "1"
*/
export const formatUSDC = (value: string | number | undefined | null): string => {
if (value === undefined || value === null || value === '') {
return '-'
}
const num = typeof value === 'string' ? parseFloat(value) : value
if (isNaN(num)) {
return '-'
}
// 使用 Math.floor 截断到4位小数(不四舍五入)
const multiplier = Math.pow(10, 4)
const truncated = Math.floor(num * multiplier) / multiplier
// 使用 toFixed(4) 确保格式一致,然后去除尾随零和小数点
return truncated.toFixed(4).replace(/\.?0+$/, '')
}
// 统一导出 ethers 相关工具函数
export {
getAddressFromPrivateKey,
getAddressFromMnemonic,
getPrivateKeyFromMnemonic,
isValidMnemonic,
isValidWalletAddress,
isValidPrivateKey
} from './ethers'