Intl Number And Currency Formatting
Format numbers, currencies, and percents with Intl.NumberFormat so locale-aware rendering needs zero manual thousand separators.
Code
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,000What 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
Phone Number FormatterNormalize a phone number with an optional country code into E.164, RFC 3966, and grouped national formats, with digit-count validation.Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.Indentation ConverterConvert leading indentation between tabs and spaces at a chosen width, leaving inner content untouched.JSON Formatter & ValidatorFormat, beautify, and validate JSON data with configurable indentation. Instantly catches syntax errors and displays a pretty-printed tree structure.
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`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.