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

fooberderp

v1.3.0

Published

Do math with money! Without risking loss of data to floating point representations!

Downloads

24

Readme

It's the FINANCIAL ARITHMETICATOOOOOOOR!

*cough*

If you're doing math on financial numbers - invoice items, tax rates, those sorts of things - you can't ever afford to represent your numbers with floating point numbers.

Store them as fixed-point types in the database, and in JavaScript, pass them around as strings to keep from possibly losing data.

Looking on npm I only found one other module that didn't taint values by converting them to or from the Number type, and it's hard-coded to a precision of two digits after the decimal point.

So I made this library. It is built on financial-arithmetic-functions, which is in turn built on the jsbin library.

I will add more methods as I need them. If you run into an operation you need that is not yet implemented, feel free to open a pull request.

Precision

This library increases the precision of the result based on its inputs.

With multiplication, the number of digits after the decimal point is the sum of the precision of both operands, e.g. 12.00 * 5.0 is 60.000.

Addition and subtraction always result in the precision of the highest-precision of the operands: 12.00 + 5 is 17.00.

Rounding

Even if you calculate the 15% tax on a 99.99$ item to be 114.9885$, that's not what you're going to print on the invoice or save to the database.

For display or storage

When you call toString to get a value to show the user, or to save in the database, you have the option of passing in a precision for the number to be displayed as.

By default, numbers will be trimmed - number('114.9885').toString(2) will return 114.98.

If you prefer rounding, you can pass in the provided rounding strategy: number('114.9885').toString(2, number.round) will produce 114.99.

If your business requirements call for a different rounding strategy, you can provide your own. I would be happy to help you write it if you open an issue.

For calculation purposes

Say somebody buys 0.50 pounds of peanuts at 5.99 a pound, their subtotal would be 2.9950. You could calculate tax based on that subtotal, but that could produce a tax a penny different from what the user would calculate themselves based on the 2.99 subtotal printed on their receipt.

When you need to change the precision in cases like this, you can use the changePrecision function, which takes the new precision (and an optional rounding strategy like toString). Like toString, it defaults to trimming.

var subtotal = number('2.9950').changePrecision(2)
subtotal.toString() // => '2.99'

Usage

npm install financial-arithmeticator, var number = require('financial-arithmeticator')


number('11.0').minus('9').times('3.75').toString() // => '7.500'
number('99.99').times('1.15').gt('100') // => true

API

  • number(string)
var numberValue = number('50.0')

Pass in the string representation of a number, get back a financial number object.

Financial numbers are immutable, and functions return a new number object.

Financial number objects have these methods. The operations and comparisons all take strings, or financial number objects.

Operations

  • numberValue.plus(num)
  • numberValue.minus(num)
  • numberValue.times(num)

Comparisons

They return true or false.

  • numberValue.gt(num)
  • numberValue.gte(num)
  • numberValue.lt(num)
  • numberValue.lte(num)
  • numberValue.equal(num)

Other utility methods

numberValue.changePrecision(newPrecision, [roundingStrategy])

Takes a new precision, and an optional rounding strategy. Returns a new number object. See Rounding

number('14.556').changePrecision(2, number.trim).toString() // => '14.55'

numberValue.toString([[displayPrecision], roundingStrategy])

Returns a string representation of the number for display or storage. You can specify the precision and rounding strategy to be passed to changePrecision if you like - by default, the number will display at its current precision. See Rounding

number('99.99').toString() // => '99.99'

numberValue.getPrecision()

number('99.99').getPrecision() // => 2

numberValue.isNegative()

number('13').isNegative() // => false
number('13').times('-1').isNegative() // => true

Running the tests/contributing

git clone https://github.com/TehShrike/financial-arithmeticator.git
cd financial-arithmeticator
npm install
npm test

Other

I never use this, so you don't have to mess around with bind to do whatever wacky functional things you want, like this:

var halved = ['10', '13', '50'].map(number('0.5').times)

Licensed WTFPL