feat: 为所有数值显示添加千分位分隔符

- 重构 formatNumber 和 formatUSDC 函数,默认添加千分位分隔符
- 更新 Statistics.tsx、CopyTradingStatistics.tsx 等统计页面
- 更新 PositionList.tsx 持仓列表的数值显示
- 更新 AccountList.tsx 账户列表的余额显示
- 所有数值(金额、数量、价格等)现在默认显示千分位
- 示例:1234567.89 显示为 1,234,567.89

影响范围:
- 工具函数:utils/index.ts
- 统计页面:Statistics.tsx, CopyTradingStatistics.tsx
- 业务页面:PositionList.tsx, AccountList.tsx

Changes:
- Refactored formatNumber and formatUSDC to include thousand separators by default
- Updated statistics pages to display numbers with commas
- Updated position list and account list for better readability
- Example: 1234567.89 now displays as 1,234,567.89
This commit is contained in:
WrBug
2026-01-31 00:52:42 +08:00
parent e8fd1b503b
commit 40081c2464
5 changed files with 510 additions and 501 deletions
+41 -41
View File
@@ -724,47 +724,47 @@ const AccountList: React.FC = () => {
{(detailAccount.totalOrders !== undefined || detailAccount.totalPnl !== undefined ||
detailAccount.activeOrders !== undefined ||
detailAccount.completedOrders !== undefined || detailAccount.positionCount !== undefined) && (
<>
<Divider />
<Descriptions
column={isMobile ? 1 : 2}
bordered
size={isMobile ? 'small' : 'middle'}
title={t('accountList.statistics')}
>
{detailAccount.totalOrders !== undefined && (
<Descriptions.Item label={t('accountList.totalOrders')}>
{detailAccount.totalOrders}
</Descriptions.Item>
)}
{detailAccount.activeOrders !== undefined && (
<Descriptions.Item label={t('accountList.activeOrdersCount')}>
<Tag color={detailAccount.activeOrders > 0 ? 'orange' : 'default'}>{detailAccount.activeOrders}</Tag>
</Descriptions.Item>
)}
{detailAccount.completedOrders !== undefined && (
<Descriptions.Item label={t('accountList.completedOrders')}>
<Tag color="success">{detailAccount.completedOrders}</Tag>
</Descriptions.Item>
)}
{detailAccount.positionCount !== undefined && (
<Descriptions.Item label={t('accountList.positionCount')}>
<Tag color={detailAccount.positionCount > 0 ? 'blue' : 'default'}>{detailAccount.positionCount}</Tag>
</Descriptions.Item>
)}
{detailAccount.totalPnl !== undefined && (
<Descriptions.Item label={t('accountList.totalPnl')}>
<span style={{
fontWeight: 'bold',
color: detailAccount.totalPnl && detailAccount.totalPnl.startsWith('-') ? '#ff4d4f' : '#52c41a'
}}>
{formatUSDC(detailAccount.totalPnl)} USDC
</span>
</Descriptions.Item>
)}
</Descriptions>
</>
)}
<>
<Divider />
<Descriptions
column={isMobile ? 1 : 2}
bordered
size={isMobile ? 'small' : 'middle'}
title={t('accountList.statistics')}
>
{detailAccount.totalOrders !== undefined && (
<Descriptions.Item label={t('accountList.totalOrders')}>
{detailAccount.totalOrders}
</Descriptions.Item>
)}
{detailAccount.activeOrders !== undefined && (
<Descriptions.Item label={t('accountList.activeOrdersCount')}>
<Tag color={detailAccount.activeOrders > 0 ? 'orange' : 'default'}>{detailAccount.activeOrders}</Tag>
</Descriptions.Item>
)}
{detailAccount.completedOrders !== undefined && (
<Descriptions.Item label={t('accountList.completedOrders')}>
<Tag color="success">{detailAccount.completedOrders}</Tag>
</Descriptions.Item>
)}
{detailAccount.positionCount !== undefined && (
<Descriptions.Item label={t('accountList.positionCount')}>
<Tag color={detailAccount.positionCount > 0 ? 'blue' : 'default'}>{detailAccount.positionCount}</Tag>
</Descriptions.Item>
)}
{detailAccount.totalPnl !== undefined && (
<Descriptions.Item label={t('accountList.totalPnl')}>
<span style={{
fontWeight: 'bold',
color: detailAccount.totalPnl && detailAccount.totalPnl.startsWith('-') ? '#ff4d4f' : '#52c41a'
}}>
{formatUSDC(detailAccount.totalPnl)} USDC
</span>
</Descriptions.Item>
)}
</Descriptions>
</>
)}
</div>
) : (
<div style={{ textAlign: 'center', padding: '20px' }}>
+8 -8
View File
@@ -3,7 +3,7 @@ import { useParams, useNavigate } from 'react-router-dom'
import { Card, Row, Col, Statistic, Tag, Button, message, Spin } from 'antd'
import { ArrowUpOutlined, ArrowDownOutlined, LeftOutlined } from '@ant-design/icons'
import { apiService } from '../services/api'
import { formatUSDC } from '../utils'
import { formatUSDC, formatNumber } from '../utils'
import { useMediaQuery } from 'react-responsive'
import type { CopyTradingStatistics } from '../types'
@@ -137,7 +137,7 @@ const CopyTradingStatisticsPage: React.FC = () => {
<Col xs={24} sm={12} md={6}>
<Statistic
title="总买入数量"
value={formatUSDC(statistics.totalBuyQuantity)}
value={formatNumber(statistics.totalBuyQuantity, 4)}
suffix=""
/>
</Col>
@@ -151,14 +151,14 @@ const CopyTradingStatisticsPage: React.FC = () => {
<Col xs={24} sm={12} md={6}>
<Statistic
title="总买入订单数"
value={statistics.totalBuyOrders}
value={formatNumber(statistics.totalBuyOrders)}
suffix="笔"
/>
</Col>
<Col xs={24} sm={12} md={6}>
<Statistic
title="平均买入价格"
value={formatUSDC(statistics.avgBuyPrice)}
value={formatNumber(statistics.avgBuyPrice, 4)}
suffix=""
/>
</Col>
@@ -171,7 +171,7 @@ const CopyTradingStatisticsPage: React.FC = () => {
<Col xs={24} sm={12} md={8}>
<Statistic
title="总卖出数量"
value={formatUSDC(statistics.totalSellQuantity)}
value={formatNumber(statistics.totalSellQuantity, 4)}
suffix=""
/>
</Col>
@@ -185,7 +185,7 @@ const CopyTradingStatisticsPage: React.FC = () => {
<Col xs={24} sm={12} md={8}>
<Statistic
title="总卖出订单数"
value={statistics.totalSellOrders}
value={formatNumber(statistics.totalSellOrders)}
suffix="笔"
/>
</Col>
@@ -198,14 +198,14 @@ const CopyTradingStatisticsPage: React.FC = () => {
<Col xs={24} sm={12} md={12}>
<Statistic
title="当前持仓数量"
value={formatUSDC(statistics.currentPositionQuantity)}
value={formatNumber(statistics.currentPositionQuantity, 4)}
suffix=""
/>
</Col>
<Col xs={24} sm={12} md={12}>
<Statistic
title="平均买入价格"
value={formatUSDC(statistics.avgBuyPrice)}
value={formatNumber(statistics.avgBuyPrice, 4)}
suffix=""
/>
</Col>
+193 -193
View File
@@ -8,7 +8,7 @@ import { getPositionKey } from '../types'
import { useMediaQuery } from 'react-responsive'
import { useWebSocketSubscription } from '../hooks/useWebSocket'
import { wsManager } from '../services/websocket'
import { formatUSDC } from '../utils'
import { formatUSDC, formatNumber as formatNumberUtil } from '../utils'
type PositionFilter = 'current' | 'historical'
type ViewMode = 'card' | 'list'
@@ -328,7 +328,7 @@ const PositionList: React.FC = () => {
if (!value) return '-'
const num = parseFloat(value)
if (isNaN(num)) return value
return num.toFixed(decimals)
return formatNumberUtil(value, decimals)
}
const formatPercent = (value: string | undefined) => {
@@ -823,132 +823,132 @@ const PositionList: React.FC = () => {
// 根据仓位类型动态生成列(历史仓位不显示当前价格、当前价值、状态列)
const columns = useMemo(() => {
const baseColumns: any[] = [
{
title: '',
key: 'icon',
width: 50,
render: (_: any, record: AccountPosition) => {
if (!record.marketIcon) return null
return (
<img
src={record.marketIcon}
alt={record.marketTitle || 'Market'}
style={{
width: '32px',
height: '32px',
borderRadius: '4px',
objectFit: 'cover'
}}
onError={(e) => {
// 图片加载失败时隐藏
e.currentTarget.style.display = 'none'
}}
/>
)
{
title: '',
key: 'icon',
width: 50,
render: (_: any, record: AccountPosition) => {
if (!record.marketIcon) return null
return (
<img
src={record.marketIcon}
alt={record.marketTitle || 'Market'}
style={{
width: '32px',
height: '32px',
borderRadius: '4px',
objectFit: 'cover'
}}
onError={(e) => {
// 图片加载失败时隐藏
e.currentTarget.style.display = 'none'
}}
/>
)
},
fixed: isMobile ? ('left' as const) : undefined
},
fixed: isMobile ? ('left' as const) : undefined
},
{
title: '账户',
dataIndex: 'accountName',
key: 'accountName',
render: (text: string | undefined, record: AccountPosition) => (
<div>
<div style={{ fontWeight: 'bold' }}>
{text || `账户 ${record.accountId}`}
</div>
<div style={{ fontSize: '12px', color: '#999', fontFamily: 'monospace' }}>
{record.walletAddress.slice(0, 6)}...{record.walletAddress.slice(-6)}
</div>
</div>
),
fixed: isMobile ? ('left' as const) : undefined,
width: isMobile ? 150 : 200
},
{
title: '市场',
dataIndex: 'marketTitle',
key: 'marketTitle',
render: (text: string | undefined, record: AccountPosition) => {
const url = record.eventSlug || record.marketSlug
? `https://polymarket.com/event/${record.eventSlug || record.marketSlug}`
: null
const handleTitleClick = (e: React.MouseEvent) => {
e.stopPropagation()
if (url) {
window.open(url, '_blank', 'noopener,noreferrer')
}
}
return (
{
title: '账户',
dataIndex: 'accountName',
key: 'accountName',
render: (text: string | undefined, record: AccountPosition) => (
<div>
{text ? (
<div>
{url ? (
<a
href={url}
target="_blank"
rel="noopener noreferrer"
onClick={handleTitleClick}
style={{ fontWeight: 'bold', color: '#1890ff', textDecoration: 'none', cursor: 'pointer' }}
>
{text}
</a>
) : (
<div style={{ fontWeight: 'bold' }}>{text}</div>
)}
</div>
) : (
<div style={{ fontFamily: 'monospace', fontSize: '12px' }}>
{record.marketId.slice(0, 10)}...
</div>
)}
{record.marketSlug && (
<div style={{ fontSize: '12px', color: '#999' }}>{record.marketSlug}</div>
)}
<div style={{ fontWeight: 'bold' }}>
{text || `账户 ${record.accountId}`}
</div>
<div style={{ fontSize: '12px', color: '#999', fontFamily: 'monospace' }}>
{record.walletAddress.slice(0, 6)}...{record.walletAddress.slice(-6)}
</div>
</div>
)
),
fixed: isMobile ? ('left' as const) : undefined,
width: isMobile ? 150 : 200
},
{
title: '市场',
dataIndex: 'marketTitle',
key: 'marketTitle',
render: (text: string | undefined, record: AccountPosition) => {
const url = record.eventSlug || record.marketSlug
? `https://polymarket.com/event/${record.eventSlug || record.marketSlug}`
: null
const handleTitleClick = (e: React.MouseEvent) => {
e.stopPropagation()
if (url) {
window.open(url, '_blank', 'noopener,noreferrer')
}
}
return (
<div>
{text ? (
<div>
{url ? (
<a
href={url}
target="_blank"
rel="noopener noreferrer"
onClick={handleTitleClick}
style={{ fontWeight: 'bold', color: '#1890ff', textDecoration: 'none', cursor: 'pointer' }}
>
{text}
</a>
) : (
<div style={{ fontWeight: 'bold' }}>{text}</div>
)}
</div>
) : (
<div style={{ fontFamily: 'monospace', fontSize: '12px' }}>
{record.marketId.slice(0, 10)}...
</div>
)}
{record.marketSlug && (
<div style={{ fontSize: '12px', color: '#999' }}>{record.marketSlug}</div>
)}
</div>
)
},
width: isMobile ? 200 : 250
},
{
title: '方向',
dataIndex: 'side',
key: 'side',
render: (side: string) => (
<Tag color={getSideColor(side)}>{side}</Tag>
),
width: 80
},
{
title: '数量',
dataIndex: 'quantity',
key: 'quantity',
render: (quantity: string) => formatNumber(quantity, 4),
align: 'right' as const,
width: 100
},
{
title: '平均价格',
dataIndex: 'avgPrice',
key: 'avgPrice',
render: (price: string) => formatNumber(price, 4),
align: 'right' as const,
width: 120
},
{
title: '开仓价值',
dataIndex: 'initialValue',
key: 'initialValue',
render: (value: string) => (
<span>
{formatUSDC(value)} USDC
</span>
),
align: 'right' as const,
width: 120
},
width: isMobile ? 200 : 250
},
{
title: '方向',
dataIndex: 'side',
key: 'side',
render: (side: string) => (
<Tag color={getSideColor(side)}>{side}</Tag>
),
width: 80
},
{
title: '数量',
dataIndex: 'quantity',
key: 'quantity',
render: (quantity: string) => formatNumber(quantity, 4),
align: 'right' as const,
width: 100
},
{
title: '平均价格',
dataIndex: 'avgPrice',
key: 'avgPrice',
render: (price: string) => formatNumber(price, 4),
align: 'right' as const,
width: 120
},
{
title: '开仓价值',
dataIndex: 'initialValue',
key: 'initialValue',
render: (value: string) => (
<span>
{formatUSDC(value)} USDC
</span>
),
align: 'right' as const,
width: 120
},
]
// 只有当前仓位才显示当前价格和当前价值列
@@ -985,70 +985,70 @@ const PositionList: React.FC = () => {
// 只有当前仓位才显示盈亏和已实现盈亏列
if (positionFilter === 'current') {
baseColumns.push(
{
title: '盈亏',
dataIndex: 'pnl',
key: 'pnl',
render: (pnl: string, record: AccountPosition) => {
const pnlNum = parseFloat(pnl || '0')
const percentPnl = parseFloat(record.percentPnl || '0')
return (
<div>
<div style={{
color: pnlNum >= 0 ? '#3f8600' : '#cf1322',
fontWeight: 'bold'
}}>
{pnlNum >= 0 ? '+' : ''}{formatUSDC(pnl)} USDC
</div>
<div style={{
fontSize: '12px',
color: percentPnl >= 0 ? '#3f8600' : '#cf1322'
}}>
{formatPercent(record.percentPnl)}
</div>
</div>
)
},
align: 'right' as const,
width: 150,
sorter: (a: AccountPosition, b: AccountPosition) => {
const pnlA = parseFloat(a.pnl || '0')
const pnlB = parseFloat(b.pnl || '0')
return pnlA - pnlB
}
},
{
title: '已实现盈亏',
dataIndex: 'realizedPnl',
key: 'realizedPnl',
render: (realizedPnl: string | undefined, record: AccountPosition) => {
if (!realizedPnl) return '-'
const pnlNum = parseFloat(realizedPnl)
const percentPnl = parseFloat(record.percentRealizedPnl || '0')
return (
<div>
<div style={{
color: pnlNum >= 0 ? '#3f8600' : '#cf1322',
fontWeight: 'bold'
}}>
{pnlNum >= 0 ? '+' : ''}{formatUSDC(realizedPnl)} USDC
</div>
{record.percentRealizedPnl && (
baseColumns.push(
{
title: '盈亏',
dataIndex: 'pnl',
key: 'pnl',
render: (pnl: string, record: AccountPosition) => {
const pnlNum = parseFloat(pnl || '0')
const percentPnl = parseFloat(record.percentPnl || '0')
return (
<div>
<div style={{
color: pnlNum >= 0 ? '#3f8600' : '#cf1322',
fontWeight: 'bold'
}}>
{pnlNum >= 0 ? '+' : ''}{formatUSDC(pnl)} USDC
</div>
<div style={{
fontSize: '12px',
color: percentPnl >= 0 ? '#3f8600' : '#cf1322'
}}>
{formatPercent(record.percentRealizedPnl)}
{formatPercent(record.percentPnl)}
</div>
)}
</div>
)
</div>
)
},
align: 'right' as const,
width: 150,
sorter: (a: AccountPosition, b: AccountPosition) => {
const pnlA = parseFloat(a.pnl || '0')
const pnlB = parseFloat(b.pnl || '0')
return pnlA - pnlB
}
},
align: 'right' as const,
width: 150
}
)
{
title: '已实现盈亏',
dataIndex: 'realizedPnl',
key: 'realizedPnl',
render: (realizedPnl: string | undefined, record: AccountPosition) => {
if (!realizedPnl) return '-'
const pnlNum = parseFloat(realizedPnl)
const percentPnl = parseFloat(record.percentRealizedPnl || '0')
return (
<div>
<div style={{
color: pnlNum >= 0 ? '#3f8600' : '#cf1322',
fontWeight: 'bold'
}}>
{pnlNum >= 0 ? '+' : ''}{formatUSDC(realizedPnl)} USDC
</div>
{record.percentRealizedPnl && (
<div style={{
fontSize: '12px',
color: percentPnl >= 0 ? '#3f8600' : '#cf1322'
}}>
{formatPercent(record.percentRealizedPnl)}
</div>
)}
</div>
)
},
align: 'right' as const,
width: 150
}
)
}
// 只有当前仓位才显示操作列
@@ -1097,7 +1097,7 @@ const PositionList: React.FC = () => {
<div style={{ marginBottom: '16px' }}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', flexWrap: 'wrap', gap: '12px', marginBottom: '12px' }}>
<div style={{ display: 'flex', alignItems: 'center', gap: '12px' }}>
<h2 style={{ margin: 0 }}></h2>
<h2 style={{ margin: 0 }}></h2>
{/* WebSocket 连接状态指示器 */}
<Tag
color={wsConnected ? 'green' : 'orange'}
@@ -1163,9 +1163,9 @@ const PositionList: React.FC = () => {
return nameA.localeCompare(nameB, 'zh-CN')
})
.map(account => ({
value: account.id,
label: account.accountName || `账户 ${account.id}`
}))
value: account.id,
label: account.accountName || `账户 ${account.id}`
}))
]}
/>
<div style={{ display: 'flex', alignItems: 'center', gap: '12px', flexWrap: 'wrap' }}>
@@ -1176,10 +1176,10 @@ const PositionList: React.FC = () => {
display: 'inline-flex',
gap: '4px'
}}>
<Radio.Group
value={positionFilter}
onChange={(e) => setPositionFilter(e.target.value)}
size={isMobile ? 'small' : 'middle'}
<Radio.Group
value={positionFilter}
onChange={(e) => setPositionFilter(e.target.value)}
size={isMobile ? 'small' : 'middle'}
style={{ display: 'flex', gap: '4px' }}
>
<Radio.Button
@@ -1215,7 +1215,7 @@ const PositionList: React.FC = () => {
{currentCount}
</Tag>
</span>
</Radio.Button>
</Radio.Button>
<Radio.Button
value="historical"
style={{
@@ -1249,8 +1249,8 @@ const PositionList: React.FC = () => {
{historicalCount}
</Tag>
</span>
</Radio.Button>
</Radio.Group>
</Radio.Button>
</Radio.Group>
</div>
{redeemableSummary && redeemableSummary.totalCount > 0 && (
<Button
+2 -2
View File
@@ -5,7 +5,7 @@ import { useTranslation } from 'react-i18next'
import type { Dayjs } from 'dayjs'
import { apiService } from '../services/api'
import type { Statistics as StatisticsType } from '../types'
import { formatUSDC } from '../utils'
import { formatUSDC, formatNumber } from '../utils'
import { useMediaQuery } from 'react-responsive'
const { RangePicker } = DatePicker
@@ -91,7 +91,7 @@ const Statistics: React.FC = () => {
<Card>
<Statistic
title={t('statistics.totalOrders') || '总订单数'}
value={stats?.totalOrders || 0}
value={formatNumber(stats?.totalOrders || 0)}
loading={loading}
/>
</Card>
+27 -18
View File
@@ -1,12 +1,14 @@
/**
*
*
* @param value -
* @param maxDecimals -
* @returns ''
* @returns "123,456.78" ''
* @example
* formatNumber(1234567.89) => "1,234,567.89"
* formatNumber(1234567.00) => "1,234,567"
* formatNumber(1234.5678, 2) => "1,234.56"
* formatNumber(100.00) => "100"
* formatNumber(100.50) => "100.5"
* formatNumber(100.55) => "100.55"
*/
export const formatNumber = (value: string | number | undefined | null, maxDecimals?: number): string => {
if (value === undefined || value === null || value === '') {
@@ -18,26 +20,38 @@ export const formatNumber = (value: string | number | undefined | null, maxDecim
return ''
}
// 如果有最大小数位数限制,先截断
// 处理小数位数
let numStr: string
if (maxDecimals !== undefined) {
const multiplier = Math.pow(10, maxDecimals)
const truncated = Math.floor(num * multiplier) / multiplier
return truncated.toFixed(maxDecimals).replace(/\.?0+$/, '')
numStr = truncated.toFixed(maxDecimals).replace(/\.?0+$/, '')
} else {
numStr = num.toString().replace(/\.?0+$/, '')
}
// 直接转换为字符串,然后去除尾随零
return num.toString().replace(/\.?0+$/, '')
// 分离整数和小数部分
const parts = numStr.split('.')
const integerPart = parts[0]
const decimalPart = parts[1]
// 为整数部分添加千分位分隔符
const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
// 组合结果
return decimalPart ? `${formattedInteger}.${decimalPart}` : formattedInteger
}
/**
* USDC
* USDC
* 4
* @param value -
* @returns '-'
* @returns "1,234.56" '-'
* @example
* formatUSDC(1234.56) => "1,234.56"
* formatUSDC(1234567.8901) => "1,234,567.8901"
* formatUSDC(1234.00) => "1,234"
* 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 => {
@@ -50,12 +64,7 @@ export const formatUSDC = (value: string | number | undefined | null): string =>
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+$/, '')
return formatNumber(num, 4)
}
// 统一导出 ethers 相关工具函数
@@ -97,7 +106,7 @@ export const isAutoGeneratedOrderId = (orderId: string | undefined | null): bool
/**
* Polymarket URL
* moneyline moneyline
* moneyline
* moneyline,
* @param marketSlug - slug
* @param eventSlug - slug events[0].slug 使
* @param marketCategory - sports, crypto