Giter Site home page Giter Site logo

bigmoney.js's Introduction

bigmoney.js

Library for handling the money values. Support multiple currencies. Based on big.js library for arbitrary-precision decimal arithmetic.

Load

The library is the single JavaScript file bigmoney.js (or bigmoney-all.min.js, which is bigmoney.js with all dependencies and minified).

It can be loaded via a script tag in an HTML document for the browser

<script src='./relative/path/to/bigmoney-all.min.js'></script>

or as a CommonJS, Node.js or AMD module using require.

For Node, put it in a node_modules directory within the directory and use require('bigmoney').

To load with AMD loader libraries such as requireJS:

require(['bigmoney'], function(BigMoney) {
    // Use BigMoney here in local scope. No global Big.
});

Use

Setup settings

Money.settings = {
    base: "USD",
    rates: {
        USD : 1, //this is base. Other rates relative to the base.
        RUB: 35.2448,
        EUR: 1/1.3485,
        JPY: 102.02
    },
    format: "%decimal %currency"
}

Constructor

var eur  = Money(100, 'EUR');
var usd  = Money(100, 'EUR').convert('USD');
var usd2 = Money(250);   //equal Money(250, Money.settings.base);

//get numeric value

usd.valueOf();             //134.85

//get string value

usd.toString();            //"134.85"

//get formatted string

usd.format();             //"134.85 USD"
usd.format("$ %decimal"); //"$ 134.85"

Convert currency

var usd = eur.convert('USD');
//or
var usd = Money(100, 'EUR').convert('USD')
//or
console.log( Money(100, 'EUR').convert('USD').format() )  //"134.85 USD"

Setup custom formatter

Money.formatter = function(decimal, currency, formatParam) {
    switch(currency) {
        case 'USD': return "$" + decimal;
        case 'EUR': return "€" + decimal;
        case 'JPY': return "¥" + decimal;
        default: return decimal + " " + currency;
    }
}

Money(100, 'EUR').format();                //"€100"
Money(100, 'EUR').convert("JPY").format(); //"¥13757.4"
Money(100, 'EUR').convert("RUB").format(); //"4752.76 RUB"

Arbitrary-precision decimal arithmetic provided by big.js library

usd.valueOf();             //134.85
usd.plus(100).valueOf();   //234.85
usd.minus(100).valueOf();  //34.85
usd.times(2).valueOf();    //269.7
usd.div(2).valueOf();      //67.43
usd.mod(1).valueOf();      //0.85

Build

For Node, if uglify-js is installed globally ( npm install uglify-js -g ) then

cat node_modules/big.js/big.js bigmoney.js | uglifyjs -o bigmoney-all.min.js

Author

Eugene Demchenko [email protected] skype: demchenkoe

Licence

See LICENCE.

Thanks

Big thanks for Michael Mclaughlin (author of big.js library).

bigmoney.js's People

Contributors

renatoargh avatar demchenkoe avatar

Watchers

 avatar James Cloos avatar

Forkers

amanasufi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.