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

goodeggs-money

v2.0.2

Published

Reliable money math with BigNumber wrapped inside Cents

Downloads

42

Readme

Goodeggs Money

Reliable money math with BigNumber wrapped inside cents

NPM version Build Status MIT License

Usage

npm install goodeggs-money
Cents = require 'goodeggs-money'

new Cents(10).toDollars() # 0.1
new Cents(10).toNumber() # 10
new Cents(10).toString() # "$0.10"

cents = Cents.fromDollars(33.44) # Cents(3344)
cents = cents.plus(1) # safe arithmetic
cents.toDollars()  # 33.45
cents.equals new Cents(3345) # true
cents.greaterThan new Cents(481) # true
cents.lessThan new Cents(8021) # true

_.sumCents([new Cents(1), new Cents(2)]) # Cents(3)

new Cents(new Cents(4)) # Cents(4); safe to re-wrap Cents
new Cents(33.44) # will throw
new Cents(-1) # will throw
new Cents(2).minus(3) # will throw
new Cents(2).minus(3, maxZero: true) # Cents(0)
new Cents(3).times(.5) # will throw
new Cents(3).times(.5, transform: 'round') # Cents(2)
new Cents(3).times(.5, transform: 'floor') # Cents(1)

Warnings

Don't use numerical comparison functions, Cents instances are objects

Built-in numerical comparisons (<, >, ==, <=, >=) don't work properly on objects. Instead use lessThan, greaterThan, equals, lessThanOrEqual, or greaterThanOrEqual.

Why?

Alleviate floating point errors

Floating-point math in computers is notorious for causing strange rounding errors. In JavaScript, this is particularly a problem. Example:

> 0.10 + 0.20
0.30000000000000004

Obviously, when dealing with money, it won't do to have rounding errors like this. They can accumulate and eventually cause weird issues. Instead, better to use whole numbers everywhere, and do math with atomic cents instead of divisible dollars.

No negative money

This module will throw an exception if you do an operation that results in negative money. Our philosophy is to prefer always representing money with positive numbers, and use variable names to express the meaning. (What does it mean for amountOwed to be negative? Not obvious. But having two variables, amountInAccount and amountOwed, is much more meaningful and explicit.)

Make things explicit

Using a module like this, every time you deal with money you'll be manipulating a Cents object instead of just manipulating a generic Number. This means that everyone encountering your code will know what your money variable is supposed to represent.

Additionally, there are operations we might want to take on money that we want to make sure happen the same way every time. This module makes these sorts of operations explicit and simple, at the cost of a bit of extra verbosity.

Support big numbers

This module uses a BigNumber implementation, which supports arbitrarily large integers. No limits on your money!

Contributing

Please follow our Code of Conduct when contributing to this project.

$ git clone https://github.com/goodeggs/goodeggs-money && cd goodeggs-money
$ npm install
$ npm test

Module scaffold generated by generator-goodeggs-npm.