Math and Intl.NumberFormat
The Math object's standard numeric operations, and Intl.NumberFormat for locale-correct display formatting instead of hand-built strings.
Math.round(n) / Math.max(...ns) / new Intl.NumberFormat(locale, options).format(n)
Purpose
Math provides standard numeric operations beyond the basic arithmetic operators. Intl.NumberFormat
formats a number for a human reader β currency, percentages, grouping separators β correctly for a
given locale, instead of hand-building the string.
Common Math members
Math.round(4.6); // 5
Math.floor(4.6); // 4
Math.ceil(4.1); // 5
Math.abs(-7); // 7
Math.max(3, 7, 2); // 7
Math.min(3, 7, 2); // 2
Math.random(); // a float in [0, 1)
Intl.NumberFormat
const usd = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });
usd.format(1234.5); // "$1,234.50"
new Intl.NumberFormat('en-US', { style: 'percent' }).format(0.42); // "42%"
new Intl.NumberFormat('de-DE').format(1234.5); // "1.234,5" β different grouping/decimal marks
Behavior notes
Math.random()never returns exactly1, only values from0up to (not including)1. A random integer in a range is built from it:Math.floor(Math.random() * (max - min + 1)) + min.Intl.NumberFormatis locale-aware: grouping separators, decimal marks, and currency symbol placement all follow the given localeβs conventions, not a fixed format.- Creating an
Intl.NumberFormatinstance has a small cost β reuse one formatter instance for many.format()calls rather than constructing a new one every time in a loop.
Common mistakes
- Hand-building a formatted price string (
'$' + n.toFixed(2)) instead of usingIntl.NumberFormatβ breaks down for other currencies, locales, and large numbers needing grouping separators. - Assuming
Math.round(-0.5)rounds toward negative infinity β it rounds half-values up (toward positive infinity), soMath.round(-0.5)is-0, not-1.
Runtime notes
Math is core ECMAScript; Intl is part of the ECMAScript Internationalization API, supported in
every current browser and Node version, though available locales can vary slightly by platform.