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

bignumber-utils

v1.0.2

Published

The bignumber-utils package empowers developers to effortlessly perform accurate and reliable decimal and non-decimal arithmetic in JavaScript, leveraging the robust foundation of the bignumber.js library.

Downloads

22

Readme

BigNumber Utils

The bignumber-utils package empowers developers to effortlessly perform accurate and reliable decimal and non-decimal arithmetic in JavaScript, leveraging the robust foundation of the bignumber.js library.

This package provides a streamlined interface for working with arbitrary-precision numbers, eliminating the limitations and potential inaccuracies associated with standard JavaScript number representation. With bignumber-utils, you can confidently handle complex calculations, financial transactions, scientific computations, and more, ensuring precise results every time.

Getting Started

Install the package:

npm install -S bignumber-utils

Usage

import {
  getBigNumber,
  isBigNumber,
  isInteger,
  isFloat
  prettifyValue,
  calculateSum,
} from 'bignumber-utils';

let value = getBigNumber('1456550199.54631546987123654159');


isBigNumber(value); // true


value = processValue(value, { 
  decimalPlaces: 18, 
  roundingMode: 'ROUND_HALF_UP',
  type: 'string'
});
// '1456550199.546315469871236542'

isBigNumber(value); // false
isInteger(value); // false
isFloat(value); // true


prettifyValue(value, { format: { prefix: '$' } });
// '$1,456,550,199.546315469871236542'

prettifyValue(value, { 
  format: { 
    groupSeparator: '.', 
    decimalSeparator: ',',
    suffix: ' BTC'
  } 
});
// '1.456.550.199,546315469871236542 BTC'


calculateSum([1, 86, '55', 46.33, '47.55', getBigNumber(8041.663321), 485, '99.11', getBigNumber(-800.654)]);
// 8061

calculateSum(
  ['0.286304850273819327', '0.00290532', '0.00251940040614675', '0.03506759540691015'], 
  { decimalPlaces: 18, type: 'string' });
// '0.326797166086876227'

API

Value Processors

  • getBigNumber instantiates BigNumber from a valid numeric value.

  • processValue processes and outputs a value to match the requirements specified in the configuration (if any).

  • prettifyValue generates the string representation of a value after being processed and formatted to match the requirements specified in the configuration (if any).

Helpers

  • isBigNumber verifies if the value is a BigNumber Instance.

  • isNumber verifies if the value is a number in any of the supported types (IBigNumberValue).

  • isInteger verifies if the value is an integer in any of the supported types (IBigNumberValue).

  • isFloat verifies if the value is a float in any of the supported types (IBigNumberValue).

Essential Calculations

  • calculateSum calculates the sum for an array of values.

  • calculateMin identifies the smallest value in an array.

  • calculateMax identifies the largest value in an array.

  • calculateMean calculates the mean for an array of values.

  • calculateMedian calculates the median for an array of values.

Percentage Calculations

  • calculatePercentageChange calculates the percentage change experienced by a value.

  • adjustByPercentage changes a value by a percentage.

  • calculatePercentageRepresentation calculates the percentage representation of a value based on a total.

Financial Calculations

  • calculateExchange calculates the asset amount that will be received once the exchange executes.

  • calculateExchangeFee calculates the fee amount that will be charged when executing a currency exchange based on a percentage.

  • calculateWeightedEntry calculates the weighted average trade price when a position can have several entries at different prices for different amounts.

BigNumber Methods

Since this library is built on top of bignumber.js, whenever you invoke getBigNumber(value) or buildNumber(value, { buildType: 'bignumber' }) you can make use of any method within the BigNumber Instace.

The list of methods can be found here

Built With

  • TypeScript

Running the Tests

$ npm run test:unit

License

MIT

Acknowledgments

@TODOS

  • [ ] Improve the docs

Deployment

Install dependencies:

$ npm install

Build the library:

$ npm start

Publish to npm:

$ npm publish