npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hailstonelabs/big-number-utils

v1.0.8

Published

hailstonelabs/big-number-utils is a library for BigNumber formatting and calculation, developed by Hailstone Labs.

Downloads

30

Readme

hailstonelabs/big-number-utils

hailstonelabs/big-number-utils is a library for BigNumber formatting and calculation, developed by Hailstone Labs.

Installation

To install and set up the library, run:

$ npm install @hailstonelabs/big-number-utils

Or if you prefer using Yarn:

$ yarn add @hailstonelabs/big-number-utils

Use

Big Number

Many operations in Ethereum operate on numbers which are outside the range of safe values to use in JavaScript.

A BigNumber is an object which safely allows mathematical operations on numbers of any magnitude. Most operations which need to return a value will return a BigNumber and parameters which accept values will generally accept them.

This library is compatible with the BigNumber exported from ether.js Source.

Safe Arithmetic

Inspiring by DS Maths Source, @hailstonelabs/big-number-utils provides arithmetic functions for the common numerical primitive types of typescript. This package provides arithmetic functions for new two higher level numerical concepts called wad (18 decimals) and ray (27 decimals). These are used to represent fixed-point decimal numbers.

WAD and RAY

A wad is a decimal number with 18 digits of precision and a ray is a decimal number with 27 digits of precision. These functions are necessary to account for the difference between how integer arithmetic behaves normally, and how decimal arithmetic should actually work.

const WAD = BigNumber.from(10).pow(18)
const RAY = BigNumber.from(10).pow(27)

DS Math API Reference

[.strToWad]

It changes string to WAD if not undefined or ParsableString, else return constants.Zero

Params

  • wadString {string | undefined}
  • returns {BigNumber}

[.getPercentageFromTwoWAD]

It returns the percentage of x in y, in string and return upperBound if the value exceeds upperBound.

Params

  • x {BigNumber}: x in WAD
  • y {BigNumber}: t in WAD
  • upperBound {BigNumber}: the upper bound percentage, in WAD
  • returns {string}: the percentage of x in y, in string
getPercentageFromTwoWAD(strToWad('20000'), strToWad('40000'))
//=> 50

getPercentageFromTwoWAD(strToWad('50000'), strToWad('40000'), strToWad('100'))
//=> 100

[.getMinValue]

Params

  • x {string}: x number in string
  • y {string}: y number in string
  • returns {BigNumber}: the minimum value in WAD

[.sum]

It returns the summation of two values.

Params

  • x {string | BigNumber}: BigNumber must be WAD
  • y {string | BigNumber}: BigNumber must be WAD
  • returns {string}: sum

Example

sum(strToWad('20000'), strToWad('30000'))
//=> 50000

[.differenceComparesValue]

It compare x - y with the value.

Params

  • x {BigNumber}
  • y {BigNumber}
  • value {BigNumber}
  • operator {'lt' | 'eq' | 'gt'} less than / equal to / greater than
  • returns {boolean}

Example

differenceComparesValue(BigNumber.from(20000), BigNumber.from(20000), 'eq')
//=> true

differenceComparesValue(BigNumber.from(40000), BigNumber.from(20000), 'lt')
//=> false

[.lessThanZeroPointZeroOne]

It checks whether x is less than 0.01.

Params

  • x {BigNumber}
  • decimal {number}
  • returns {boolean}

Example

lessThanZeroPointZeroOne(BigNumber.from(0.00003), 5))
//=> true

[.changeDecimal]

It changes tokenA amount (with tokenA decimal) to target value (with tokenB decimal).

Params

  • fromDecimal {number}
  • toDecimal {number}
  • fromAmount {BigNumber}
  • returns {BigNumber}

[.nativeToWAD]

Params

  • x {BigNumber} x in BigNumber
  • decimal {number}
  • returns {BigNumber}: convertion to WAD

[.wadToNative]

Params

  • x {BigNumber} x in WAD
  • decimal {number}
  • returns {BigNumber}: convertion to Origin

[.bnIntToWAD]

Params

  • x {BigNumber} in BigNumber Int
  • returns {BigNumber}: convertion to WAD

[.bnIntToRAY]

Params

  • x {BigNumber} x in BigNumber Int
  • returns {BigNumber}: convertion to RAY

[.wadToRay]

Params

  • x {BigNumber} x in WAD
  • returns {BigNumber}: convertion to RAY

[.rayToWad]

Params

  • x {BigNumber} x in RAY
  • returns {BigNumber}: convertion to WAD

[.wmul]

Params

  • x {BigNumber} x in WAD
  • y {BigNumber} y in WAD
  • returns {BigNumber}: the product of x and y, in WAD

[.wdiv]

Params

  • x {BigNumber} x in WAD
  • y {BigNumber} y in WAD
  • returns {BigNumber}: the quotient of x divided by y, in WAD

[.safeWdiv]

Params

  • x {BigNumber} x in WAD
  • y {BigNumber} y in WAD
  • returns {BigNumber}: the quotient of x divided by y, in WAD

[.safeDiv]

Params

  • x {BigNumber} x in BigNumber
  • y {BigNumber} y in BigNumber
  • returns {BigNumber}: the quotient of x divided by y, in BigNumber

[.rmul]

Params

  • x {BigNumber} x in RAY
  • y {BigNumber} y in RAY
  • returns {BigNumber}: the product of x and y, in RAY

[.rpow]

Params

  • x {BigNumber} x in RAY
  • n {BigNumber}
  • returns {BigNumber}: the exponential of x to the power n, in RAY

[.sqrt]

Params

  • x {BigNumber} x in BigNumber Int
  • returns {BigNumber}: the square root of x, in BigNumber Int

[.wsqrt]

Params

  • x {BigNumber} x in WAD
  • returns {BigNumber}: the square root of x, in WAD

Parsable String API Reference

[.isParsableString]

Check whether bnString can be parsed as a BigNumber.

Params

  • bnString {string}
  • dp {number} the decimals
  • isNonNegativeOnly {boolean} whether the string is only a non negative value
  • returns {boolean}: whether the string is parsable
isParsableString(strToWad('100000000'), 18, false)
//=> true

isParsableString(strToWad('100000000000000000000'), 18, false)
//=> false

[.getParsableString]

Params

  • bnString {string}
  • dp {number} the decimals
  • isNonNegativeOnly {boolean}
  • returns {BigNumber}: If bnString is parsable, return bnStrin, else return 0

Number Display Format API Reference

[.getStringInput]

If a value is in WAD, it will format the value.

Params

  • value {string | BigNumber}: a value in string or in WAD
  • returns {string}: a formatted string value

Example

getStringInput(strToWad('20000'))
//=> 20000

[.getDpFormat]

It trim extra decimals in order to avoid fractional component exceeds decimals.

Params

  • value {string | BigNumber}: a value in string or in WAD
  • decimalPlace {number}: decimal place, the default is 2
  • rounding {'down'|'off'}: decimal place, the default is down
  • returns {string}: a decimal trimmed value

Example

getDpFormat(strToWad('1.1234567'), 6)
//=> 1.123456

getDpFormat(strToWad('1.1234567'), 6, 'off')
//=> 1.123457

[.getMillifiedFormat]

It converts long numbers to human-readable string.

Params

  • value {string | BigNumber}: a value in string or in WAD

  • returns {string}: a millified value with 1 d.p.

Example

getMillifiedFormat(strToWad('1424000'))
//=> 1.4M

getMillifiedFormat(strToWad('2500'))
//=> 2.5K

[.getCommifiedFormat]

It always checks whether an actualValue is less than 0.01.

Params

  • actualValue {string | BigNumber}: a value in string or in WAD
  • decimalPlace {number}: decimal place, the default is 2
  • returns {string}: readable string, rounded to x decimal places or if the actualValue is less than 0.01, it returns "< 0.01"

Example

getCommifiedFormat(strToWad('0.00007'))
//=> < 0.01

getCommifiedFormat(strToWad('1255354.664'))
//=> 1,255,354.66

[.getDynamicFormat]

If actualValue is greater than or equal to 100000 return millified format value, else return commified format value.

Params

  • actualValue {string | BigNumber}: a value in string or in WAD
  • decimalPlace {number}: decimal place, the default is 2
  • returns {string}: readable string with millified format or commified format

Example

getDynamicFormat(strToWad('1024000'))
//=> 1.02M

getDynamicFormat(strToWad('90000'))
//=> 90,000