formatRange Renders Number Ranges

formatRange renders two numbers as one localized range with collapsed units and percents, instead of concatenating two separately formatted values.

JavascriptData Integrity

Code

javascriptsnippet
20 lines
// 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 – 20

Inside 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

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`.