数字格式化与本地化
学习如何在界面中友好地展示数值:千位分隔、小数位、货币、百分比等。
Intl.NumberFormat 基础
js
const n = 1234567.891;
// 千分位 & 保留两位小数
new Intl.NumberFormat("zh-CN", { minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(n);
// 货币(人民币)
new Intl.NumberFormat("zh-CN", { style: "currency", currency: "CNY" }).format(n);
// 百分比
new Intl.NumberFormat("zh-CN", { style: "percent", maximumFractionDigits: 1 }).format(0.756);
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
自定义格式
js
function formatFixed(num, digits = 2) {
return Number(num).toFixed(digits); // 字符串
}
1
2
3
2
3