formatRange Renders Number Ranges
formatRange renders two numbers as one localized range with collapsed units and percents, instead of concatenating two separately formatted values.
Code
// formatRange renders two numbers as one localized range instead of
// concatenating two separately formatted values.
const nf = new Intl.NumberFormat('en-US');
console.log(nf.formatRange(1200, 5600));
// Units collapse the same way: a single "1–5 GB", not "1 GB – 5 GB"
const disk = new Intl.NumberFormat('en-US', { style: 'unit', unit: 'gigabyte' });
console.log(disk.formatRange(1, 5));
// Percent ranges keep a % on both ends of the dash
const pct = new Intl.NumberFormat('en-US', {
style: 'percent',
maximumFractionDigits: 1,
});
console.log(pct.formatRange(0.123, 0.456));
// Date ranges work through Intl.DateTimeFormat.formatRange
const df = new Intl.DateTimeFormat('en-US', { month: 'short', day: 'numeric' });
console.log(df.formatRange(new Date('2026-11-01'), new Date('2026-11-20')));Output
Captured from a sandboxed run of this snippet in the build pipeline.
1,200–5,600
1–5 GB
12.3% – 45.6%
Nov 1 – 20Inside the snippet
The file’s state is a handful of top-level bindings: nf, disk, pct, df.
Caveats
- The environment needs no preparation beyond having the interpreter or compiler.
Key statements worth quoting
const disk = new Intl.NumberFormat('en-US', { style: 'unit', unit: 'gigabyte' });
const df = new Intl.DateTimeFormat('en-US', { month: 'short', day: 'numeric' });
const pct = new Intl.NumberFormat('en-US', {
style: 'percent',
maximumFractionDigits: 1,
});
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
- Which packages does this snippet need?
- None. Every symbol the code uses resolves inside the standard library. Its state lives in module-level bindings: `nf`, `disk`, `pct`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.