Money$afe
Convenient, safe money calculations in JS.
Status - Developer Preview
Money$afe has not yet been tested in production at scale.
What is it?
Writing software that deals with money is a bit of a pain in JavaScript. Money-safe calculations are harder than they should be.
Why? Because JavaScript Numbers are IEEE 754 64-bit floating point. The result is that we can't safely add money because the decimal will get skewered by floating point rounding errors.
2 + 1 === 3; // false
However, this problem effectively goes away if you perform the same calculations in arbitrary precision units. Money$afe converts your dollar values into BigNumbers and then exposes arithetic operations like add, multiply, and divide.
With Money$afe:
=== ;
Even better. There's a convenient ledger form for common calculations like shopping carts:
; // 88
Known Issues
This code was written in ES6, and no attempt has been made to compile it for older browsers. This should not be a problem in any modern evergreen browser, but it will likely throw errors in old IE and old versions of Safari. You have been warned. If you want to run the code in other browsers, you'll need to compile to ES5 yourself.
Values are stored in arbitrary precision using BigNumber, so you can perform accurate calculations for cryptocurrencies such as Bitcoin or Ethereum which have 8 and 18 decimal precision, respectively. By way of contrast, JavaScript's native number type is IEEE 754 with 16 digits of decimal precision.
To recap:
- By default, all math operations automatically use arbitrary precision big numbers internally.
- You can get the value in a number type using
.toNumber()
.
Getting Started
Install moneysafe:
npm install --save moneysafe
Import the functions you need:
;;
OR:
const $ = ;const $$ subtractPercent addPercent = ;
Enjoy:
; // 88
How does Money$afe work?
It works by storing and acting on the amounts in cents instead of dollars, which reduces the floating point rounding errors you get when you represent them as decimal dollars. Of course, you'll still get rounding errors with lots of multiplication and division, but errors are less common and less significant when scaled to cents.
$(dollars) => Money
The $()
factory takes a value in dollars and lifts it into the money object type.
Money
Example:
cents; // 2000
Once a value is represented as money, you can operate on it using normal JavaScript operators. The resulting value will be in cents:
/ 2; // 1000 cents
in$ Utility
Take a numerical value in cents and convert to a numerical value in dollars, rounded to the nearest cent.
in$(cents: n) => dollars: Number
Since Money$afe allows you to use normal math operators, which work in cents, in$()
is a convenient way to convert the result back to dollars:
; in // 10
$ Static Props
$.of()
Takes a value and lifts it into the Money
object type. Not rounded.
$ Money
Example:
$; // 20.2$; // 1.635$; // 0.30000000000000004
The Money Type
The Money type is a function object returned by the createCurrency
factory. The type itself is a function that takes an amount in number or string format and returns a new Money
object.
Money
Example:
const a = ;const b = ; const c = ;console; // 30
The result is that standard function composition acts like addition. The following are equivalent:
;; const a = ; const b = ; const c = ; console; // 30 const c = ; console;
This is what makes the handy ledger syntax possible. $$
is just a thin wrapper around a standard function composition:
; + // 88
money.add()
Takes an amount and returns a money instance with the sum of the stored value and the amount.
money Money
Example:
// 15
money.minus()
Takes an amount and returns a money instance with the difference between the stored value and the amount.
money Money
Example:
// 5 // 5 // -5
money.toNumber(), money.valueOf()
Convert a Money
object to JavaScript Number format (IEEE 754 floating point). Note: JavaScript number precision is limited to 16 decimal digits.
money Number
Example:
; // 2000
money.abs()
Returns a Money
object which contains the absolute value.
money Money
Example:
=== ; // true
money.toString()
Convert a Money
object to a String
. Warning: This isn't a properly localized currency string suitable for display to users. Please use a good i18n library and/or exchange rate API to convert to localized currency.
money String
Example:
; // "2000"
money.map()
Apply a function of type BigNumber => BigNumber
in the context of the Money object.
This allows you to implement arbitrary operations for Money objects, which you
can apply by mapping them. Note: money.map()
obeys the functor laws.
money Money
Example:
const pow = Array; +; // 4
Utility functions
add()
Take any number of money objects and return the sum.
Money
Example:
add($('0.1'), $('0.2')).toString() === '0.30'; // true
multiply()
Take any number of money objects and return the product.
Money
Example:
=== '8.00'; // true
Divide
Take a dividend and divisor and return the quotient.
Money
Example:
=== '4.00'; // true
Less Than
Take a base and a comparand and return whether the comparand is less than the base.
boolean
Example:
=== true; // true === false; // false === false; // false
Greater Than
Take a base and a comparand and return whether the comparand is greater than the base.
boolean
Example:
=== false; // false === false; // false === true; // true
Less Than or Equal to
Take a base and a comparand and return whether the comparand is less than or equal the base.
boolean
Example:
=== true; // true === true; // true === false; // false
Greater Than or Equal to
Take a base and a comparand and return whether the comparand is greater than or equal the base.
boolean
Example:
=== false; // false === true; // true === true; // true
$$ Ledger
Takes any number of money objects (or functions of type Money => Money
) and returns a money object containing the sum.
Money
Example:
;; ; // 95
addPercent()
Takes a percent x
as a number and the current value in cents (curried), and returns a new money object representing the sum of the current value and x%
of the current value.
Money
Example:
;; const total = ; console;
subtractPercent()
Takes a percent x
as a number and the current value in cents (curried), and returns a new money object representing the difference between the current value and x%
of the current value.
Money
Example:
;; const total = ; console;