Const
The number to clamp
The minimum allowed value
The maximum allowed value
The clamped number within the specified range
NumberUtils.clamp(10, 0, 5); // Returns 5
NumberUtils.clamp(-10, 0, 5); // Returns 0
NumberUtils.clamp(3, 0, 5); // Returns 3
Formats a number as currency using Intl.NumberFormat.
The number to format as currency
The locale to use for formatting (default: 'en-US')
The currency code (default: 'USD')
The formatted currency string
NumberUtils.formatCurrency(1234.56); // Returns "$1,234.56"
NumberUtils.formatCurrency(1000, 'de-DE', 'EUR'); // Returns "1.000,00 €"
NumberUtils.formatCurrency(500, 'ja-JP', 'JPY'); // Returns "¥500"
Formats a number with locale-appropriate thousands separators.
The number to format
The locale to use for formatting (default: 'en-US')
The formatted number string with thousands separators
NumberUtils.formatWithThousandsSeparator(1234567); // Returns "1,234,567"
NumberUtils.formatWithThousandsSeparator(1234567, 'de-DE'); // Returns "1.234.567"
NumberUtils.formatWithThousandsSeparator(1234567, 'fr-FR'); // Returns "1 234 567"
Checks if a number is between two values (inclusive).
The number to check
The minimum value (inclusive)
The maximum value (inclusive)
True if the number is between min and max (inclusive), false otherwise
NumberUtils.isBetween(5, 1, 10); // Returns true
NumberUtils.isBetween(0, 1, 10); // Returns false
NumberUtils.isBetween(10, 1, 10); // Returns true
Checks if a number is prime using an optimized algorithm.
The number to check for primality
True if the number is prime, false otherwise
NumberUtils.isPrime(17); // Returns true
NumberUtils.isPrime(4); // Returns false
NumberUtils.isPrime(2); // Returns true
NumberUtils.isPrime(1); // Returns false
Generates a random integer between min and max (inclusive).
The minimum value (inclusive)
The maximum value (inclusive)
A random integer between min and max
NumberUtils.randomInt(1, 6); // Returns a random number between 1 and 6 (like a dice roll)
NumberUtils.randomInt(10, 20); // Returns a random number between 10 and 20
Rounds a number to a specified number of decimal places.
The number to round
The number of decimal places (default: 0)
The rounded number
NumberUtils.round(3.14159, 2); // Returns 3.14
NumberUtils.round(2.7); // Returns 3
NumberUtils.round(1.005, 2); // Returns 1.01
Formats a number with a fixed number of decimal places.
The number or string to format
The number of decimal places to display
The formatted number as a string with fixed decimal places
NumberUtils.formatWithDecimals(3.14159, 2); // Returns "3.14"
NumberUtils.formatWithDecimals(5, 3); // Returns "5.000"
NumberUtils.formatWithDecimals("invalid", 2); // Returns "0.00"
Truncates a number to a specified number of decimal places (no rounding).
The number to truncate
The number of decimal places to keep (default: 0)
The truncated number
NumberUtils.truncate(3.14159, 2); // Returns 3.14
NumberUtils.truncate(2.99, 1); // Returns 2.9
NumberUtils.truncate(5.999); // Returns 5
Clamps a number between a minimum and maximum value (inclusive).