getDigit()getDigits()iDigits()integrate()interpolate()
isApproximately()isBetween()isValidNumInputString()lineLength()round()roundDown()roundDownMult()roundMult()roundUp()roundUpMult()
The numeric utility functions and constants are useful for manipulating numbers. All utility functions and constants are properties of the global/window object and may be used without importing.
addAverage() addAverage(avg, value, n)
Add a new value to an average. n is the total number of samples, including this one. Returns the updated average. For example:
avg = addAverage(avg, value, ++n)
avgvaluenA number. The updated average.
adjust360() adjust360(deg)
Return deg adjusted so that the angle is equivalent and > 0 but ≤ 360.
degThe adjusted number.
angleDiff() angleDiff(a, b)
Return the shortest angle difference in degrees (±180) from a to b. Positive values mean b is clockwise from a. Negative values mean b is counter-clockwise from a.
abA number in degrees between -180 and 180.
average() average(...args)
Returns the average of all the arguments.
argsA number. The average of all the arguments.
clamp() clamp(value, min, max)
Returns the value constrained to be within the range min to max.
valueminmaxA number. The value clamped to be within the range min to max.
epsilonEqual() epsilonEqual(a, b)
Returns true if numbers a and b are within EPSILON of each other.
abA Boolean. True if a and b are within EPSILON of each other.
isAngleIn() isAngleIn(a, start, end)
Return true if angle a in degrees is between start moving clockwise to end.
astartendA Boolean. True if a in degrees is between start moving clockwise to end.
fmtNum() fmtNum(num)
fmtNum(num, digits)
Returns num as a string with thousands separators. If digits is present and greater than 0, then the result is rounded to that number of digits and that number of digits appear to the right of the decimal.
numdigits (optional)A string containing the formatted num.
fmtRange() fmtRange(start, end)
fmtRange(start, end, unit)
fmtRange(start, end, unit, sep)
Returns a string representing a numeric range from start to end. If unit is present, it is a string containing a unit abbreviation that is included in the range. If sep is present, it is a string containing a separator that is placed between the start and end values in the formatted range. The default separator is '-'. For example:
fmtRange(0, 10)
returns 0 - 10.
fmtRange(0, 10, 'kg.', 'to')
returns 0 kg. to 10 kg.. If start is equal to end, then only the start and any unit is returned.
startendunit (optional)start and end values.sep (optional)start and end values if they are different.A string containing the formatted range.
getDigit() getDigit(value, digit)
Extracts a single decimal digit from value. A digit value of zero specifies the rightmost integer digit, increasing for digits to the left of the decimal point and decreasing for digits to the right of the decimal point. For example:
getDigit(12.34, -1) == 3
valuedigitdigit is zero, it specifies the lowest-order integer digit.An integer number.
getDigits() getDigits(value, digit, len)
Extracts decimal digits from value. A digit value of zero specifies the rightmost integer digit, increasing for digits to the left of the decimal point and decreasing for digits to the right of the decimal point. len specifies the number of digits to extract from the starting point moving right. For example:
getDigit(12.34, 1, 2) == 12
valuedigitdigit is zero, it specifies the lowest-order integer digit.lenAn integer number.
iDigits() iDigits(value)
Returns the number of integer digits in value.
valueAn integer number.
integrate() integrate(fn, from, to, incr)
Return the sum of a function over a numeric interval. The function fn(start, size) is called with a start value beginning at from, then is repeatedly called with new start values by adding incr to the last start value, stopping when the entire interval between from and to is covered. integrate() returns the sum of the values returned by calling fn().
The size argument to fn() indicates the size of the sub-interval. For all sub-intervals except the last, this is the incr value. If incr does not evenly divide the interval from from to to, then the last call to fn() will be the size of the partial sub-interval remaining.
The from value can be greater than the to value provided incr is negative.
fnfn(start, size). The parameters are:startsizestart to the end of this sub-interval. All sub-intervals are of size incr except for the last, which may be less than incr if incr does not device the distance between the to and from values evenly.fn returns a numeric value to be added to the final summation.fromtoincrA number that's the sum of all the returned values of fn() for all sub-intervals.
interpolate() interpolate(x, x1, y1, x2, y2)
Returns the Y value associated with the x value by linearly interpolating a line on a two-dimensional plane between points (x1, y1) and (x2, y2). The x value must be between x1 and x2.
xx1y1x2y2The numeric Y value of the x value on a line between (x1, y1) and (x2, y2).
isAngleIn() isAngleIn(a, start, end)
Return true if angle a in degrees is between start moving clockwise to end.
astartendA Boolean that's true if a is in the angle between start moving clockwise to end.
isApproximately() isApproximately(value1, value2, ratio)
Returns true if value2 is within approximation bounds of value1. The approximation bounds are:value1 ± (ratio * value1).
For example:
isApproximately(1, 1.1, 0.2) === true
value1value2ratiovalue1 that is the approximation bounds.A boolean that's true if value2 falls within the approximation bounds of value1.
isBetween() isBetween(value, min, max)
Returns true if value is greater than or equal to min and less than or equal to max.
valueminmaxA boolean that's true if value falls within the specified range.
isValidNumInputString() isValidNumInputString(str)
Return true if the str string is valid number or a prefix of a valid number.
strA boolean.
lineLength() lineLength(x1, y1, x2, y2)
Returns the length of the line from (x1, y1) to (x2, y2).
x1y1x2y2A number. The line length.
round() round(value)
round(value, digits)
If digits is zero or omitted, rounds to the nearest integer value. Otherwise, if digits is a positive integer, it rounds to the nearest digits places to the right of the decimal point. For example:
round(12.34, 1) === 12.3
If digits is a negative integer, it rounds to the nearest -digits places to the left of the decimal point. For example:
round(123.45, -1) === 120
valuedigits (optional)value is rounded to the nearest integer. If positive, the value is rounded to the nearest digits places to the right of the decimal point. If negative, the value is rounded to the nearest -digits places to the left of the decimal point.The rounded number.
roundDown() roundDown(value)
roundDown(value, digits)
If digits is zero or omitted, rounds down to the next integer value. Negative numbers round lower. For example:
roundDown(-12.34) === -13
If digits is a positive integer, it rounds down to the next digits places to the right of the decimal point. For example:
roundDown(12.34, 1) === 12.3
If digits is a negative integer, it rounds down to the next -digits places to the left of the decimal point. For example:
roundDown(123.45, -1) === 120
valuedigits (optional)value is rounded down to the next lower integer. If positive, the value is rounded to the next lower digits places to the right of the decimal point. If negative, the value is rounded to the next lower -digits places to the left of the decimal point.The rounded number.
roundDownMult() roundDownMult(value, mult)
Rounds down to the next multiple of mult. For example:
roundDownMult(123.45, 20) === 120
and
roundDownMult(123.45, .5) === 123
valuemultThe rounded number.
roundMult() roundMult(value)
roundMult(value, mult)
Rounds to the nearest multiple of mult. For example:
roundMult(123.45, 20) === 120
and
roundMult(123.45, .5) === 123.5
If mult is a string, it contains a number to be used as the multiple followed by a 'd', or 'u' to round up or down, respectively. For example:
roundMult(123.45, '20u') === 140
and
roundMult(123.45, '.5d') === 123
valuemult (optional)'d', or 'u'. If 'd', then the function rounds down to the next lower multiple. If 'u', then the function rounds up to the next higher multiple. If the string only contains a number, then that number is used as the multiple. If mult is omitted, then mult is assumed to be 1.The rounded number.
roundUp() roundUp(value)
roundUp(value, digits)
If digits is zero or omitted, rounds up to the next integer value. Negative numbers round higher. For example:
roundUp(-12.34) === -12
If digits is a positive integer, it rounds up to the next digits places to the right of the decimal point. For example:
roundUp(12.34, 1) === 12.4
If digits is a negative integer, it rounds up to the next -digits places to the left of the decimal point. For example:
roundUp(123.45, -1) === 130
valuedigits (optional)value is rounded up to the next higher integer. If positive, the value is rounded to the next higher digits places to the right of the decimal point. If negative, the value is rounded to the next higher -digits places to the left of the decimal point.The rounded number.
roundUpMult() roundUpMult(value, mult)
Rounds up to the next multiple of mult. For example:
roundUpMult(123.45, 20) === 140
and
roundUpMult(123.45, .5) === 123.5
valuemultThe rounded number.
weightedAverage() weightedAverage(values, weights)
Returns the average of the numbers in the values array. Each value is weighted by the corresponding weight in the weights array. The weights do not need to add up to 1.
valuesvaluesA number. The weighted average of all the values.
EPSILONThe value used to assume rough equality in epsilonEqual(). This is a small number but quite a bit bigger than Number.MIN_VALUE. It is a practical smallest number for printed non-exponential fractions and is used to prevent useless arithmetic differences due to the way JavaScript numbers are represented.