Library for operating with monetary values in JavaScript and Typescript 💵
Library for operating with monetary values in JavaScript and Typescript
import { createMoney } from '@easymoney/money';const money1 = createMoney({ amount: 100, currency: 'USD' });
const money2 = createMoney({ amount: 106, currency: 'USD' });
const money3 = money1.add(money2).getAmount(); // => 206
import { createMoneyIntlFormatter } from "@easymoney/formatter" import { createMoney } from '@easymoney/money';const money = createMoney({ amount: 5, currency: "USD" });
const formatted = createMoneyIntlFormatter().format(money); // => "$0.05"
import { createMoneyCryptoFormatter } from "@easymoney/crypto-formatter" import { createMoney } from '@easymoney/money'; import { cryptoCurrenciesMap } from "@easymoney/currencies"const money = createMoney({ amount: 5, currency: "LTC" });
const formatted = createMoneyCryptoFormatter().format(money); // => "0.00000005LTC"
const currencies = [{ minorUnit: 2, code: "XBT" }, { minorUnit: 5, code: "DXBT" }];const list = createCurrencyList(currencies);
list.getCurrencies(); /**
npm i @easymoney/money
or
yarn add @easymoney/money
JavaScript developers attempt to use just numbers or strings (i.g. https://github.com/MikeMcl/bignumber.js) to operate with monetary values. I don't think this is the right way, and strings or numbers are not well suited for financial applications for these main reasons:
The solution to these problems is the Martin Fowler's Money Type from "Patterns of Enterprise Application Architecture".
It's an old pattern that is implemented in many other languages e.g.:
Further reading
How to Operate with Monetary Values in JavaScript
Why rounding is more important than you think
| | easymoney | Dinero.js | | --------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | | Typescript | ✅ | ❌ (only @types/Dinero) | | Modular | functionality is divided by domains, which allows the library to be divided into separate modules as efficiently as possible | written as a monolith, one big object with a bunch of functions, no way to divide it into packages by design | | Custom currencies support | ✅ | ❌ (only ISO currencies) | | Custom calculators support | ✅ | ❌ | | Monetary values greater than MAXSAFEINTEGER | ✅ | ❌ | | Bigint support | ✅ | ❌ |
| Package | Version | Dependencies | Size | | :-----------------------------: | :----------------------------------------------: | :------------------------------------------------: | :----------------------------------------------: | |
@easymoney/bigint-money|
@easymoney/bignumber.js|
@easymoney/currencies|
@easymoney/money|
@easymoney/formatter|
@easymoney/crypto-formatter|
Thanks goes to these wonderful people (emoji key):
Andrey Frolov ⚠️ 📖 💻 |
oneyan1 ⚠️ 📖 💻 |
Mikchail 📖 |
Roman Martynenko 📖 |
Artem676 📖 |
This project follows the all-contributors specification. Contributions of any kind welcome!