# moneysafe

2.2.1 • Public • Published

# 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.

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:

### 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:

Import the functions you need:

OR:

Enjoy:

## 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.

Example:

Once a value is represented as money, you can operate on it using normal JavaScript operators. The resulting value will be in cents:

## in\$ Utility

Take a numerical value in cents and convert to a numerical value in dollars, rounded to the nearest cent.

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:

### \$ Static Props

#### \$.of()

Takes a value and lifts it into the `Money` object type. Not rounded.

Example:

## 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.

Example:

The result is that standard function composition acts like addition. The following are equivalent:

This is what makes the handy ledger syntax possible. `\$\$` is just a thin wrapper around a standard function composition:

Takes an amount and returns a money instance with the sum of the stored value and the amount.

Example:

### money.minus()

Takes an amount and returns a money instance with the difference between the stored value and the amount.

Example:

### 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.

Example:

### money.abs()

Returns a `Money` object which contains the absolute value.

Example:

### 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.

Example:

### 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.

Example:

## Utility functions

Take any number of money objects and return the sum.

Example:

``````add(\$('0.1'), \$('0.2')).toString() === '0.30'; // true
``````

## multiply()

Take any number of money objects and return the product.

Example:

## Divide

Take a dividend and divisor and return the quotient.

Example:

## Less Than

Take a base and a comparand and return whether the comparand is less than the base.

Example:

## Greater Than

Take a base and a comparand and return whether the comparand is greater than the base.

Example:

## Less Than or Equal to

Take a base and a comparand and return whether the comparand is less than or equal the base.

Example:

## Greater Than or Equal to

Take a base and a comparand and return whether the comparand is greater than or equal the base.

Example:

## \$\$ Ledger

Takes any number of money objects (or functions of type `Money => Money`) and returns a money object containing the sum.

Example:

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.

Example:

### 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.

Example:

## Package Sidebar

### Install

`npm i moneysafe`

### Repository

github.com/ericelliott/moneysafe

1,321

2.2.1

MIT

22.5 kB

13