Alt description missing in image
Early access: More plugins soon!
Alt description missing in image

Number Utils

import * as numberUtils from '@green-stack/utils/numberUtils'

Collection of utility functions for number manipulation and validation:

        • numberUtils.ts

Random Number Utils

randomInt()

Generates a random integer between a max & min range.

// Generate a random number between 0 and 10
const result = randomInt(10) // => 7
 
// Generate a random number between 5 and 10
const result2 = randomInt(10, 5) // => 8

Rounding Utils

roundTo()

Uses scientific string notation to round numbers to specific decimals. Can pass a specific math rounding function as third arg.

// Round to 2 decimal places using default rounding
roundTo(3.14159, 2) // => 3.14
 
// Round to 2 decimal places using ceiling
roundTo(3.14159, 2, Math.ceil) // => 3.15
 
// Round to 2 decimal places using floor
roundTo(3.14159, 2, Math.floor) // => 3.14

roundUpTo()

Convenience function to round up to specific decimals.

roundUpTo(3.14159, 2) // => 3.15

roundDownTo()

Convenience function to round down to specific decimals.

roundDownTo(3.14159, 2) // => 3.14

Number Formatting Utils

formatPercent()

Turns a decimal number into a percentage string, optionally rounding to specific decimals.

// Basic usage
formatPercent(0.5) // => '50%'
formatPercent(0.75) // => '75%'
 
// With decimal precision
formatPercent(0.12345, 2) // => '12.35%'
formatPercent(0.666666, 1) // => '66.7%'
 
// With custom rounding function
formatPercent(0.12345, 2, roundUpTo) // => '12.35%'
formatPercent(0.12341, 2, roundDownTo) // => '12.34%'

formatMoney()

Turns a float number into a formatted money string.

// Basic usage
formatMoney(1000) // => '$ 1,000.00'
formatMoney(1234567.89) // => '$ 1,234,567.89'
 
// Custom currency symbol
formatMoney(1000, '€') // => '€ 1,000.00'
formatMoney(1000, '£') // => '£ 1,000.00'
 
// Custom decimal places
formatMoney(1000, '$', 0) // => '$ 1,000'
formatMoney(1000.5, '$', 3) // => '$ 1,000.500'
 
// Without grouping separators
formatMoney(1000000, '$', 2, false) // => '$ 1000000.00'

Number Validation Utils

hasLeadingZeroes()

Detects leading zeroes in “numbers”.

hasLeadingZeroes('0568') // => true
hasLeadingZeroes('568') // => false
hasLeadingZeroes('0') // => false

isValidNumber()

Checks if a string is a valid number, excluding leading zeroes.

// Valid numbers
isValidNumber('0') // => true
isValidNumber('1') // => true
isValidNumber('250') // => true
isValidNumber('0.123') // => true
isValidNumber(42) // => true
 
// Invalid numbers
isValidNumber('0568') // => false (leading zero)
isValidNumber('abc') // => false (not a number)
isValidNumber('') // => false (empty string)
isValidNumber(null) // => false (null)
isValidNumber(undefined) // => false (undefined)
isValidNumber(NaN) // => false (NaN)

parseIfValidNumber()

Checks if a variable can be parsed as a valid number, and then parses that number.

// Valid numbers
parseIfValidNumber('42') // => 42
parseIfValidNumber('0.123') // => 0.123
parseIfValidNumber(42) // => 42
 
// Invalid numbers
parseIfValidNumber('0568') // => undefined
parseIfValidNumber('abc') // => undefined
parseIfValidNumber('') // => undefined
parseIfValidNumber(null) // => undefined
parseIfValidNumber(undefined) // => undefined
parseIfValidNumber(NaN) // => undefined