Number Utilities
Formatting numbers as currency, bytes, percentages, and other common numeric helpers that developers always need to look up.
formatCurrency
Format a number as a currency string.
const formatCurrency = (amount, currency = "USD", locale = "en-US") =>
new Intl.NumberFormat(locale, { style: "currency", currency }).format(amount);
formatCurrency(1234567.89); // "$1,234,567.89"
formatCurrency(1234567.89, "EUR", "de-DE"); // "1.234.567,89 €"
formatCurrency(1500, "INR", "en-IN"); // "₹1,500.00"
formatCurrency(9.99, "GBP", "en-GB"); // "£9.99"
formatNumber
Format a number with thousands separators and decimal places.
const formatNumber = (n, decimals = 0, locale = "en-US") =>
new Intl.NumberFormat(locale, { minimumFractionDigits: decimals, maximumFractionDigits: decimals }).format(n);
formatNumber(1234567); // "1,234,567"
formatNumber(1234567.891, 2); // "1,234,567.89"
formatNumber(0.5, 1); // "0.5"
formatBytes
Convert bytes into a human-readable size string (KB, MB, GB…).
const formatBytes = (bytes, decimals = 2) => {
if (bytes === 0) return "0 Bytes";
const k = 1024;
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(decimals))} ${sizes[i]}`;
};
formatBytes(0); // "0 Bytes"
formatBytes(1024); // "1 KB"
formatBytes(1048576); // "1 MB"
formatBytes(1073741824); // "1 GB"
formatBytes(1500, 1); // "1.5 KB"
formatCompact
Compact large numbers (e.g. 1K, 2.5M).
const formatCompact = (n, locale = "en-US") =>
new Intl.NumberFormat(locale, { notation: "compact", compactDisplay: "short" }).format(n);
formatCompact(1000); // "1K"
formatCompact(1500); // "1.5K"
formatCompact(1000000); // "1M"
formatCompact(2500000); // "2.5M"
formatCompact(1000000000); // "1B"
clamp
Clamp a number between a min and max value.
const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
clamp(15, 0, 10); // 10
clamp(-5, 0, 10); // 0
clamp(5, 0, 10); // 5
randomInt
Generate a random integer between min and max (inclusive).
const randomInt = (min, max) =>
Math.floor(Math.random() * (max - min + 1)) + min;
randomInt(1, 6); // dice roll: 1–6
randomInt(0, 100); // 0–100
randomFloat
Generate a random float between min and max.
const randomFloat = (min, max, decimals = 2) =>
parseFloat((Math.random() * (max - min) + min).toFixed(decimals));
randomFloat(0, 1); // 0.73 (random)
randomFloat(1.5, 3.5, 1); // 2.4 (random)
lerp
Linear interpolation between two values.
const lerp = (start, end, t) => start + (end - start) * t;
lerp(0, 100, 0.5); // 50
lerp(10, 20, 0.25); // 12.5
lerp(0, 255, 1); // 255
isEven / isOdd
const isEven = (n) => n % 2 === 0;
const isOdd = (n) => n % 2 !== 0;
isEven(4); // true
isOdd(7); // true
toOrdinal
Convert a number to its ordinal string (1st, 2nd, 3rd…).
const toOrdinal = (n) => {
const s = ["th", "st", "nd", "rd"];
const v = n % 100;
return n + (s[(v - 20) % 10] || s[v] || s[0]);
};
toOrdinal(1); // "1st"
toOrdinal(2); // "2nd"
toOrdinal(3); // "3rd"
toOrdinal(11); // "11th"
toOrdinal(21); // "21st"
toOrdinal(42); // "42nd"
percentage
Calculate a percentage and format it.
const percentage = (part, total, decimals = 1) =>
`${((part / total) * 100).toFixed(decimals)}%`;
percentage(25, 200); // "12.5%"
percentage(1, 3, 2); // "33.33%"
percentage(750, 1000); // "75.0%"
roundTo
Round a number to N decimal places.
const roundTo = (n, decimals) =>
Math.round(n * 10 ** decimals) / 10 ** decimals;
roundTo(1.2345, 2); // 1.23
roundTo(1.005, 2); // 1.01
roundTo(123.456, 0); // 123
Summary
| Function | Input | Output |
|---|---|---|
formatCurrency(1234.5) | 1234.5 | "$1,234.50" |
formatNumber(1234567, 2) | 1234567 | "1,234,567.00" |
formatBytes(1048576) | 1048576 | "1 MB" |
formatCompact(1500000) | 1500000 | "1.5M" |
clamp(15, 0, 10) | 15, 0, 10 | 10 |
randomInt(1, 6) | 1, 6 | 1–6 |
lerp(0, 100, 0.5) | 0, 100, 0.5 | 50 |
toOrdinal(21) | 21 | "21st" |
percentage(25, 200) | 25, 200 | "12.5%" |
roundTo(1.2345, 2) | 1.2345, 2 | 1.23 |