Number methods
Explore the list of utility methods for working with numbers.
The numberUtils
library provides various utility functions for working with numbers.
Info
numberUtils
library uses Lodash's Number methods. For full details of how to use these methods, refer to Lodash documentation.
Methods reference in numberUtils library
numberUtils.clamp(source, lower, upper)
Clamps a number within the specified range.
Parameters
number
(number): The number to be clamped.lower
(number): The lower bound of the range.upper
(number): The upper bound of the range.
Returns
- (number): The clamped number.
Example
const clampedNumber = numberUtils.clamp(5, 10, 20);
// Result: 10
numberUtils.inRange(source, start, end)
Checks if a number is within the specified range.
Parameters
number
(number): The number to check.start
(number): The start of the range.end
(number): The end of the range.
Returns
- (boolean): true if the number is within the range, false otherwise.
Example
const isInRange = numberUtils.inRange(15, 10, 20);
// Result: true
numberUtils.random([lower=0], [upper=1], [floating])
Generates a random number within the specified range.
Parameters
lower
(number): Optional. The lower bound of the range.upper
(number): Optional. The upper bound of the range.floating
(boolean): Optional. Whether to generate a floating-point number.
Returns
- (number): The generated random number.
Example
const randomInteger = numberUtils.random(1, 10);
// Result: (e.g., 7)
const randomFloat = numberUtils.random(0, 1, true);
// Result: (e.g., 0.548)