NumberFormat Options: Precision and Rounding
You'll learn how to fine-tune number displays by controlling decimal places, significant digits, and rounding behavior using Intl.NumberFormat options.
Why Precision Matters
When formatting numbers, you often need exact control over how many digits appear. Financial apps might require exactly two decimal places, while scientific calculators need significant figures. Intl.NumberFormat gives you multiple ways to specify precision.
Controlling Fraction Digits
The minimumFractionDigits and maximumFractionDigits options control decimal places after the point:
const precise = new Intl.NumberFormat('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
console.log(precise.format(42)); // "42.00" (pads to minimum)
console.log(precise.format(3.14159)); // "3.14" (rounds to maximum)
Think of these as guardrails: the minimum ensures you always show at least that many digits (padding with zeros if needed), while the maximum caps how many can appear (rounding if necessary).
Significant Digits
For scientific notation or when total precision matters more than decimal position, use minimumSignificantDigits and maximumSignificantDigits:
const scientific = new Intl.NumberFormat('en-US', {
minimumSignificantDigits: 3,
maximumSignificantDigits: 3
});
console.log(scientific.format(1.2)); // "1.20"
console.log(scientific.format(12345)); // "12,300" (rounds after 3 digits)
Combining with Other Options
Precision options work alongside currency and unit formatting:
const money = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
console.log(money.format(9.5)); // "$9.50"
Key Takeaway: Use fraction digit options for decimal precision and significant digit options for overall numerical precision—both automatically handle rounding according to locale conventions.