Skip to content

@pmun/utils / formatCurrency

Function: formatCurrency() ​

ts
function formatCurrency(value, options): string;

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

Parameters ​

value ​

数字或十进制数字字符串(允许首尾空白),也支持 null、undefined

undefined | null | string | number

options ​

FormatCurrencyOptions = {}

格式化选项,默认为 {},省略时按 'zh-CN' 格式显示人民币符号并保留 2 位小数

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.56, { maximumFractionDigits: 0 }) // '¥1,235',四舍五入为整数
formatCurrency(1234, { minimumFractionDigits: 0 }) // '¥1,234'
formatCurrency(1234.56, { minimumFractionDigits: 4 }) // '¥1,234.5600'
formatCurrency('1234.56') // '¥1,234.56',支持数字字符串
formatCurrency(1234.56, { showSymbol: false }) // '1,234.56'
formatCurrency(null) // ''
formatCurrency(undefined) // ''
formatCurrency(' ') // ''