Common number and math helpers.
- SWR Audio Lab / Numbers
- Install
addLeadingZero
- add leading zero to numberaddTrailingZeros
- add trailing zeros to numbergetAverage
- get average from array of numbersgetDiff
- get difference of two numbersgetRandomInRange
- get random int between min and maxgetSum
- get sum from array of numbersisEven
- check if a value is evennormalize
- normalize a value by given maximumroundTo
- round float to a specified decimal placetoReadable
- get a number in readable format
Add the parent package to your dependencies:
yarn add @swrlab/utils
value
(required) - Value to add leading zero
Import the library:
const { addLeadingZero } = require('@swrlab/utils/packages/numbers')
Then use the toolkit:
addLeadingZero(1)
// '01'
addLeadingZero(10)
// '10'
value
(required) - Value to add trailing zeroslength
(required) - Amount of the digits after delimiterdelimiter
(optional) - Delimiter for trailing zeros (default .)
Import the library:
const { addTrailingZeros } = require('@swrlab/utils/packages/numbers')
Then use the toolkit:
addTrailingZeros(1, 5)
// '1.00000'
addTrailingZeros(1.1, 5)
// '1.10000'
addTrailingZeros('1.2', 5)
// '1.20000'
addTrailingZeros(2, 2, ',')
// '2,00'
addTrailingZeros(2.1, 2, ',')
// '2,10'
addTrailingZeros('2,2', 2, ',')
// '2,20'
array
(required) - Array of numbersdecimal
(optional) - Decimal places to round (default 2)
Import the library:
const { getAverage } = require('@swrlab/utils/packages/numbers')
Then use the toolkit:
getAverage([1, 2, 3])
// 2
getAverage([1.2, 2.4, 3.6], 1)
// 2.4
value a
(required) - value to subtract fromvalue b
(required) - value to subtract
Import the library:
const { getDiff } = require('@swrlab/utils/packages/numbers')
Then use the toolkit:
getDiff(2, 1)
// 1
getDiff(1, 2)
// -1
min
(required) - Min value (inclusive)max
(required) - Max value (inclusive)
Import the library:
const { getRandomInRange } = require('@swrlab/utils/packages/numbers')
Then use the toolkit:
getRandomInRange(1, 5)
// 1,2,3,4 or 5
getRandomInRange(5, 9)
// 5,6,7,8 or 9
array
(required) - Array of numbersdecimal
(optional) - Decimal places to round (default 2)
Import the library:
const { getSum } = require('@swrlab/utils/packages/numbers')
Then use the toolkit:
getSum([1, 2, 3])
// 6
getSum([1.2, 2.4, 3.6], 1)
// 7.2
value
(required) - Value to check
Import the library:
const { isEven } = require('@swrlab/utils/packages/numbers')
Then use the toolkit:
isEven(2)
// true
isEven(1)
// false
value
(required) - Value to normalizemax
(required) - Maximum for normalization
Import the library:
const { normalize } = require('@swrlab/utils/packages/numbers')
Then use the toolkit:
normalize(2, 100)
// 0.02
normalize(80, 100)
// 0.8
value
(required) - Float value to rounddecimal
(optional) - Decimal places to round (default 2)
Import the library:
const { roundTo } = require('@swrlab/utils/packages/numbers')
Then use the toolkit:
roundTo(1.23456)
// 1.23
roundTo(1.23456, 4)
// 1.2346
value
(required) - Value to convert
Import the library:
const { toReadable } = require('@swrlab/utils/packages/numbers')
Then use the toolkit:
toReadable(1234567)
// '1.234.567'