Skip to content

@pmun/utils / formatCurrency

Function: formatCurrency()

ts
function formatCurrency(value, options): string;

将数字格式化为货币字符串

Parameters

value

要格式化的数字,支持 number 或数字字符串(如 '1234.56')

string | number

options

格式化选项

currency?

string

货币代码(如 'CNY', 'USD'),默认为 'CNY'

locale?

string

地区设置(如 'zh-CN', 'en-US'),默认为 'zh-CN'

maximumFractionDigits?

number

最大小数位数,默认为 2

minimumFractionDigits?

number

最小小数位数,默认为 2

showSymbol?

boolean

是否显示货币符号,默认为 true

Returns

string

格式化后的货币字符串

Example

ts
formatCurrency(1234.56) // '¥1,234.56'
formatCurrency(1234.56, { currency: 'USD', locale: 'en-US' }) // '$1,234.56'
formatCurrency(1234.56789, { maximumFractionDigits: 4 }) // '¥1,234.5679'
formatCurrency(1234, { minimumFractionDigits: 0 }) // '¥1,234'
formatCurrency('1234.56') // '¥1,234.56',支持数字字符串
formatCurrency(1234.56, { showSymbol: false }) // '1,234.56'