Intl Number And Currency Formatting

Format numbers, currencies, and percents with Intl.NumberFormat so locale-aware rendering needs zero manual thousand separators.

JavascriptData Integrity

Code

javascriptsnippet
17 lines
const amount = 1234567.891;

// 货币:语言标签决定千分位与小数位惯例
const usd = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });
const eur = new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' });
console.log(usd.format(amount));
console.log(eur.format(amount));

// 百分比与紧凑记法
const pct = new Intl.NumberFormat('en-US', { style: 'percent', maximumFractionDigits: 1 });
console.log(pct.format(0.1234));
const compact = new Intl.NumberFormat('en-US', { notation: 'compact' });
console.log(compact.format(amount));

// 自定义精度:significantDigits vs maximumFractionDigits
const precise = new Intl.NumberFormat('en-US', { maximumSignificantDigits: 4 });
console.log(precise.format(amount));

Output

Captured from a sandboxed run of this snippet in the build pipeline.

$1,234,567.89
1.234.567,89 €
12.3%
1.2M
1,235,000

What the code does

Top-level bindings amount, usd, eur, pct hold the demo state — nothing sits inside a function scope.

Statements that matter

const pct = new Intl.NumberFormat('en-US', { style: 'percent', maximumFractionDigits: 1 });
const usd = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });
const eur = new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' });

Interactive Tools for This Topic

FAQ

Does the code pull in any external packages?
No external packages are involved; every call resolves to a built-in API. Its state lives in module-level bindings: `amount`, `usd`, `eur`.