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

dirac-number

v3.0.1

Published

A collection of utility methods for floating point Number operations

Downloads

6

Readme

dirac-number

travis npm js-standard-style

A collection of utility methods for floating point Number operations

numbers

dirac-number is a dirac-flavoured wrapper for BigNumber.js.

Instead of constructing BigNumber objects from your numbers and performing operations on them within your app, this module provides a set of simplified math utilities that take simple Number or String types, and use BigNumber operations for accuracy behind the scenes.

API

DiracNumber is an extension of the BigNumber class.

import DiracNumber from 'dirac-number'

const num = new DiracNumber('123.456')
num.round(2) // '123.46'
num instanceof BigNumber // true

There is a shortcut method of construction in DiracNumber.vn:

import DiracNumber, { vn } from 'dirac-number'

const num = vn(123.456)
num.round(2) // '123.46'
num instanceof DiracNumber // true

Math operations

The following static methods can take any number of values and perform the operation on each of them in the specified order.

Add a list of values

DiracNumber.add(1, 2, 3) // 1 + 2 + 3 = 6

Subtract a list of values

DiracNumber.subtract(2, 1) // 2 - 1 = 1

Multiply a list of values

DiracNumber.multiply(2, 2) // 2 * 2 = 4

Divide a list of values

DiracNumber.divide(10, 2) // 10 / 2 = 5

Keep in mind when using the DiracNumber math functions that when performing multiple calculations you will need to make sure the execution order is correct.

Native JS will execute in order of BEDMAS automatically, whereas with these methods, you have to make sure of this manually.

E.g.

4 * 5 + 3 // returns 23

multiply(4, add(5, 3)) // returns 32

You need to take into account the order in which the functions will be executed, so in this case it should be:

add(3, multiply(4, 5)) // returns 23

Rounding and formatting

Round a value to a specified number of decimal points. The default rounding mode is ROUND_HALF_UP, the 'Round Half Away From Zero' tie-breaking rule.

DiracNumber.round(5.545333, 2) // "5.55"

Rounding modes

You can specify any of the Big Number rounding modes to the round function. These are available via DiracNumber.ROUNDING_METHODS.

import DiracNumber, { ROUNDING_MODES } from 'dirac-number'

DiracNumber.round(5.545333, 2, ROUNDING_MODES.ROUND_DOWN) // "5.54"

isFinite

A static equivalent to BigNumber.isFinite that reports false for non-numeric values.

DiracNumber.isFinite(null) // false
DiracNumber.isFinite('') // false
DiracNumber.isFinite(Infinity) // false

DiracNumber.isFinite(0) // true
DiracNumber.isFinite(Number.MAX_VALUE) // true
DiracNumber.isFinite('-123.456') // true

sumBy

Calculates the sum of all items in a collection based on a property name.

Accepts the following parameters:

  • Array an array of items to loop through.
  • String a property name to sum by.
  • Number an optional decimal points number to round the sum value to (defaults to 2 d.p.).

Example:

DiracNumber.sumBy([], '') // 0.00
DiracNumber.sumBy([{ a: 1 }, { a: 2 }], 'a') // 3.00
DiracNumber.sumBy([{ a: 1.4222 }, { a: 2.1115 }], 'a', 3) // 3.534

Contributing

Build

npm run build

Test

npm test

Watch

To watch for changes, build them and run the tests:

npm run watch

License

MIT © Dirac