2.4.14 • Published 4 months ago

monval v2.4.14

Weekly downloads
3
License
MIT
Repository
github
Last release
4 months ago

monval

Precise and simple money utility with exchange rates support.

monval uses Gaussian rounding when exporting values in a fixed length. Currently, the gaussian method is the only supported rounding method but the module flexible enough to provide more rounding methods in future releases.

Install

npm i monval

Import

There are exports for es6, cjs and umd environments:

// cjs
const {monval} = require('monval')

// es
import {monval} from 'monval'

// script tag, window.monval
<script src="https://cdn.jsdelivr.net/npm/monval@2/dist/monval.iife.js" type="text/javascript"></script>

Usage

// start with ten dollars in a pocket
const account = monval.create(10, 'USD')

// incoming 100.789$
account.add(100.789)

// 21.66$ spent!
account.subtract(21.66)

// 10 percent increase!
account.add('%10')

// 20 percent discount!
account.subtract('%20')

// The value always stored as float.
assert.strictEqual(account.toFloat(), 78.43352)
// toFixed method rounds the value according to the rounding algorithm and
// the decimal length standart to the currency
assert.strictEqual(account.toFixed(), '78.43')

// pretty print
monval.create(123.4567, 'EUR').pretty() // € 123.46
monval.create(123.4567, 'EUR').pretty(4) // € 123.4567

Let's load exchange rates:

const sampleExchangeRates = {/* EUR: 0.99, TRY: 1.11 ... */}
monval.exchangeRatesBaseCurrency = 'USD'
monval.exchangeRates = sampleExchangeRates.rates

// convert our usd to try
account.exchange('TRY')
// the pocket now has
assert.strictEqual(account.toFloat(), 478.16211132800004)
// turkish liras
assert.strictEqual(account.money.currency, 'TRY')

account.pretty() // ₺ 478.16

// add more dollars into the pocket!
account.add(100, 'usd')
// now we have 1087.8 try!
assert.strictEqual(acount.toFixed(), '1087.8')

Additional features:

monval.isCurrency('XXX') // false
monval.isCurrency('EUR') // true

Contributing

If you're interested in contributing, read the CONTRIBUTING.md first, please.


Version management of this repository done by releaser 🚀


Thanks for watching 🐬

Support me on Patreon

2.4.14

4 months ago

2.4.13

10 months ago

2.4.10

10 months ago

2.4.12

10 months ago

2.4.11

10 months ago

2.4.7

10 months ago

2.4.6

10 months ago

2.4.9

10 months ago

2.4.8

10 months ago

2.4.5

1 year ago

2.3.0

1 year ago

2.2.1

1 year ago

2.2.0

1 year ago

2.4.1

1 year ago

2.4.0

1 year ago

2.4.3

1 year ago

2.4.2

1 year ago

2.4.4

1 year ago

2.1.0

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.6

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.3.0

2 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago