项目作者: dleitee

项目描述 :
💳A little library to handle money amounts.
高级语言: JavaScript
项目地址: git://github.com/dleitee/walletjs.git
创建时间: 2017-02-17T01:30:33Z
项目社区:https://github.com/dleitee/walletjs

开源协议:MIT License

下载


“Mo money, No problems” - walletjs

Build Status
Coverage Status
Code Climate

IMPORTANT: We use big.js to handle huge numbers.

Now you can handle money without headaches!

API Reference

Install

  1. npm install --save walletjs
  2. or
  3. yarn add walletjs

Examples

  1. import Wallet, { Money } from 'walletjs'
  2. const money = Money.init(100)
  3. const wallet = Wallet.init(money)
  4. console.log(wallet.getAmount(money.currency))
  5. const money2 = Money.init(100)
  6. const newWallet = wallet.add(money2)
  7. console.log(newWallet.getAmount(money2.currency))

convertCurrency

  1. import Wallet, { Money } from 'walletjs'
  2. const money = Money.init(100, { currency: 'BRL' } )
  3. const brlWallet = Wallet.init(money)
  4. const usdWallet = brlWallet.convertCurrency('BRL', 'USD', 3.09)
  5. console.log(brlWallet.getAmount('BRL')) => '100.00'
  6. console.log(brlWallet.getAmount('USD')) => '0.00'
  7. console.log(usdWallet.getAmount('BRL')) => '0.00'
  8. console.log(usdWallet.getAmount('USD')) => '309.00'

no problems with float errors

  1. // on javascript
  2. const a = 0.1
  3. const b = 0.2
  4. console.log(a + b) => 0.30000000000000004
  5. // on walletjs
  6. const money = Money.init(0.2)
  7. money.add(0.1) => returns 0.3