Skip to content

Latest commit

 

History

History

numbers

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

SWR Audio Lab / Numbers

Common number and math helpers.

Install

Add the parent package to your dependencies:

yarn add @swrlab/utils

addLeadingZero - add leading zero to number

  • 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'

addTrailingZeros - add trailing zeros to number

  • value (required) - Value to add trailing zeros
  • length (required) - Amount of the digits after delimiter
  • delimiter (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'

getAverage - get average from array of numbers

  • array (required) - Array of numbers
  • decimal (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

getDiff - get difference of two numbers

  • value a (required) - value to subtract from
  • value 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

getRandomInRange - get random int between min and max

  • 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

getSum - get sum from array of numbers

  • array (required) - Array of numbers
  • decimal (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

isEven - check if a value is even

  • 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

normalize - normalize a value by given maximum

  • value (required) - Value to normalize
  • max (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

roundTo - round float to a specified decimal place

  • value (required) - Float value to round
  • decimal (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

toReadable - get a number in readable format

  • value (required) - Value to convert

Import the library:

const { toReadable } = require('@swrlab/utils/packages/numbers')

Then use the toolkit:

toReadable(1234567)
// '1.234.567'