Skip to content

jsanchesleao/math-money

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Math Money

Math money is a module aimed for safe currency manipulation, by not operating with pure floating points.

Installation

    npm install math-money

Quickstart

    var Money = require('math-money');

    var oneDollar = Money.Dollar(1); //create using an integer
    var oneEuro = Money.Euro('12.00'); //create using a string

    var result = Money.Dollar(12).plus( Money.Dollar('10.00') );
    console.log( result.format() ); // US$ 22.00

Defining Your Currency

    var Money = require('math-money');
    var MyCurrency = Money.factory('MyCurrency', {
        decimals: 2,
        prefix: 'My$'
    });

    console.log( MyCurrency(10).format() ); //My$ 10.00

To create a custom currency, use Money.factory method. It accepts the currency identifier as first parameter and a config object as second, with the following fields:

  • decimals (how many decimal places the currency supports)
  • prefix (the prefix to format the currency. Normally you should put the currency symbol)

Attributes

  • intValue

Money.Dollar(1).intValue Should return 100. This is because US Dollars use 2 decimal places, like US$ 1.00, so, in order to avoid floating point math, 100 is returned.

Operations

Money objects are immutable, so all math operations will not modify the object itself, but return another one.

  • Plus

Money.Dollar(1).plus( Money.Dollar(2) )
Should return a Money.Dollar(3);

  • Minus

Money.Dollar(2).minus( Money.Dollar(1) )
Should return a Money.Dollar(1)

  • isMoreThan

Money.Dollar(2).isMoreThan( Money.Dollar(1) )
Should return a true if money value is bigger that the compared one

  • isLessThan

Money.Dollar(1).isLessThan( Money.Dollar(2) )
Should return a true if money value is lower that the compared one

  • equals

Money.Dollar(1).equals( Money.Dollar(1) )
Should return a true if money value and the compared one are equal

Different currencies cannot interoperate. If you try to add, subtract or compare different currencies, an exception will be thrown.

Convenience Methods and Attributes

  • Currency.raw
    var Money = require('math-money');
    Money.Dollar.raw( 2000 ); // US$ 20.00
    Money.Yen.raw(2000); // ¥ 2000

This generates a Money object built with a raw integer value. This value will then be converted to decimal if needed.

  • Currency.ZERO
    var Money = require('math-money');
    Money.Dollar.ZERO; // US$ 0.00
    Money.BRL.ZERO; // R$ 0.00
    Money.Yen.ZERO; // ¥ 0

Returns a money value representing no money.

Built in currencies

math-currency currently supports these currencies out of the box:

  • Brazilian Real Money.BRL
  • Euro Money.Euro or Money.EUR
  • Japanese Yen Money.Yen or Money.JPY
  • US Dollar Money.Dollar or Money.USD)

About

Floating point free Money objects and operations. Safe for math.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published