Intl.NumberFormat Rounding Options
roundingPriority picks between fraction and significant digit limits, roundingMode picks the direction, and trailingZeroDisplay trims whole numbers only.
Code
// roundingPriority decides between fraction and significant digit limits
// when both are set on the same formatter.
const more = new Intl.NumberFormat('en-US', {
maximumSignificantDigits: 3,
maximumFractionDigits: 2,
roundingPriority: 'morePrecision',
});
const less = new Intl.NumberFormat('en-US', {
maximumSignificantDigits: 3,
maximumFractionDigits: 2,
roundingPriority: 'lessPrecision',
});
console.log('more:', more.format(1234.5));
console.log('less:', less.format(1234.5));
// roundingMode picks how values beyond the limit round
const floor = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0, roundingMode: 'floor' });
const ceil = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0, roundingMode: 'ceil' });
console.log('floor 2.9:', floor.format(2.9), '| ceil 2.1:', ceil.format(2.1));
// trailingZeroDisplay hides decimal padding on whole numbers only
const trimmed = new Intl.NumberFormat('en-US', {
minimumFractionDigits: 2,
trailingZeroDisplay: 'stripIfInteger',
});
console.log('2.0:', trimmed.format(2.0), '| 2.5:', trimmed.format(2.5));Output
Captured from a sandboxed run of this snippet in the build pipeline.
more: 1,234.5
less: 1,230
floor 2.9: 2 | ceil 2.1: 3
2.0: 2 | 2.5: 2.50What the code does
Demo state sits in top-level bindings: more, less, floor, ceil.
Points of care
- No third-party code is loaded at any point while this runs.
Using it
Output lines are prefixed (more:, less:, floor 2.9:), making each step’s effect easy to spot.
Key statements
const floor = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0, roundingMode: 'floor' });
const ceil = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0, roundingMode: 'ceil' });
const trimmed = new Intl.NumberFormat('en-US', {
minimumFractionDigits: 2,
trailingZeroDisplay: 'stripIfInteger',
});
Interactive Tools for This Topic
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.JSON SorterSort JSON object keys by code point ascending or descending, recursively or top-level only, preserving array order.
FAQ
- Is the dependency story complicated?
- Not at all: zero dependencies, so there is nothing to version or audit. Its state lives in module-level bindings: `more`, `less`, `floor`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.