概述
Intl.NumberFormat 提供对数字的本地化格式,支持货币与单位展示、紧凑表示(如 1.2K)、分组与小数控制。适用于多语言站点与报表。
示例
const nfCurrency = new Intl.NumberFormat('zh-CN', { style: 'currency', currency: 'CNY', currencyDisplay: 'symbol' })
console.log(nfCurrency.format(1234.56))
const nfUnit = new Intl.NumberFormat('zh-CN', { style: 'unit', unit: 'kilometer', unitDisplay: 'short' })
console.log(nfUnit.format(3.5))
const nfCompact = new Intl.NumberFormat('zh-CN', { notation: 'compact', maximumFractionDigits: 1 })
console.log(nfCompact.format(1200)) // ≈ 1.2K
工程建议
- 货币与单位:按地区使用正确货币代码与单位;避免硬编码符号。
- 性能:缓存格式器实例;避免在渲染循环中新建。
- 兼容:不支持选项时回退到自定义格式化;保持语义一致。
参考与验证
- MDN Intl.NumberFormat 文档:https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
- ECMA-402 规范:https://tc39.es/ecma402/#numberformat-objects

发表评论 取消回复