THIS REPOSITORY IS NO LONGER MAINTAINED.
Underscore.number
Number-related functions missing from underscore.js.
Installation
npm
npm install underscore.number
bower
bower install underscore.number
Functions
sign _.num.sign(num)
Returns a sign of num
as -1
, 0
or 1
.
_num; // -1_num; // 0_num; // 1
pinch _.num.pinch(num, min, max)
Puts num
in a range of min
to max
.
Returns min
if num
is smaller than min
, max
if num
is larger than max
, and num
itself if it’s already in a range of min
to max
.
_num; // 10_num; // 5_num; // 0
loop _.num.loop(num, min, max)
Converts num
as it is in a circular range of min
to max
(excluding max
).
Considering the expression, _.num.loop(480, 0, 360)
as an angle calculation, it converts 480 degrees in a range of 0 to 360 degrees.
_num; // 120_num; // 240_num; // 180_num; // 0_num; // 360_num; // -120
clockwise _.num.clockwise(from, to, range)
Returns the clockwise distance from from
to to
as they are in a circle of range
. Useful to calculate angles.
_num; // 100_num; // 260_num; // 0_num; // 359
nearer _.num.nearer(from, to, range)
Returns the nearer distance from from
to to
as they are in a circle of range
. Useful to calculate angles.
_num; // 100_num; // -100_num; // 0_num; // -1
average _.num.average(*nums)
Returns an average of nums
_num; // 2.5
round _.num.round(num, level)
Rounds num
by specified level
.
_num; // 0_num; // 100_num; // 120_num; // 123 (same as Math.round(123.456))_num; // 123.5_num; // 123.46_num; // 123.456
between _.num.between(from, to, ratio)
_num; // 0_num; // 50_num; // 100
add _.num.add(*nums)
Adds nums
.
_num; // 10
sub _.num.sub(base, *nums)
Subtracts nums
from base
.
_num; // 4
mul _.num.mul(*nums)
Multiplies nums
.
_num; // 60
div _.num.div(base, *nums)
Divides base
by nums
.
_num; // 4
random _.num.random([a[, b]])
Returns a random number.
_num; // (same as Math.random())_num; // random number in range of 0 to 5 (excluding 5)_num; // random number in range of 2 to 4 (excluding 4)
radToDeg _.num.radToDeg(radians)
Converts radians to degrees.
_num; // 180
degToRad _.num.degToRad(degrees)
Converts degrees to radians.
_num; // 3.141592653589793 (Math.PI)
License
MIT