
Product
Introducing Reports: An Extensible Reporting Framework for Socket Data
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.
goodeggs-money
Advanced tools
Reliable money math with BigNumber wrapped inside cents
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)
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.
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!
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.
FAQs
Reliable money math with BigNumber wrapped inside Cents
The npm package goodeggs-money receives a total of 21 weekly downloads. As such, goodeggs-money popularity was classified as not popular.
We found that goodeggs-money demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.