Inspired from CASA Lib / AS 2.0 (v.10/06/08) - Aaron Clinger, David Nelson
manmaru.numberutil.js :
Utilities for manipulating number.
isEqual(val1, val2, precision) ->
Determines if the two values are equal, with the option to define the precision
@param val1: A value to compare
@param val2: A value to compare
@param precision: The maximum amount the two values can differ and still be considered equal
@return Returns true the values are equal; otherwise false
min(val1, val2) ->
Evaluates val1
and val2
and returns the smaller value
Unlike Math.min
this method will return the defined value if the other value is null or not a number
@param val1: A value to compare
@param val2: A value to compare
@return Returns the smallest value, or the value out of the two that is defined and valid
randomWithinRange(min, max) ->
Creates a random number within the defined range
@param min: The minimum value the random number can be
@param min: The maximum value the random number can be
@return Returns a random number within the range
randomIntegerWithinRange(min, max) ->
Creates a random integer within the defined range
@param min: The minimum value the random integer can be
@param min: The maximum value the random integer can be
@return Returns a random integer within the range
isEven(value) ->
Determines if the number is even
@param value: A number to determine if it is divisible by 2
@return Returns true if the number is even; otherwise false
isOdd(value) ->
Determines if the number is odd
@param value: A number to determine if it is not divisible by 2
@return Returns true
if the number is odd; otherwise false
addLeadingZero(value) ->
Adds a leading zero for numbers less than ten
@param value: Number to add leading zero
@return Number as a String; if the number was less than ten the number will have a leading zero
isInteger(value) ->
Determines if the number is an integer
@param value: A number to determine if it contains no decimal values
@return Returns true if the number is an integer; otherwise false
isPrime(value) ->
Determines if the number is prime
@param value: A number to determine if it is only divisible by 1 and itself
@return Returns true if the number is prime; otherwise false
roundDecimalToPlace(value, place) ->
Rounds a number's decimal value to a specific place
@param value: The number to round
@param place: The decimal place to round
@return Returns the value rounded to the defined place
NumberUtil.roundToPlace(3.14159, 2)); // 3.14
NumberUtil.roundToPlace(3.14159, 3)); // 3.142
isBetween(value, firstValue, secondValue) ->
Determines if the value is included within a range
@param value: Number to determine if it is included in the range
@param firstValue: First value of the range
@param secondValue: Second value of the range
@return Returns true if the number falls within the range; otherwise false
@usageNote The range values do not need to be in order.
NumberUtil.isBetween(3, 0, 5)); // true
(NumberUtil.isBetween(7, 0, 5)); // false