Giter Club home page Giter Club logo

money-math's Introduction

Build Status

What does it do?

Adds, multiplies the currency amounts, and calculates percentages of amounts. The result of each of those operations is also an amount: a string, strictly matching the /^\-?\d+\.\d\d$/ pattern, like "0.25", "1000.00", or "-42.10".

Amounts on input and output are arbitrary large and precise:

99999999999999999999999999999999999999999999999999999999999999999999999999999999.99
+
0.01
=
100000000000000000000000000000000000000000000000000000000000000000000000000000000.00

However, in cases when the division is involved โ€” like for percentage calculation โ€” the result is rounded to the whole cent.

money.percent("0.50", "33.00")  // is "0.17" instead of "0.165"

As a bonus feature, there's a simple formatting function for amounts in CHF, EUR, USD, GBP, and JPY.

money.format("EUR", "-1560.00") // "-1.560,00"

Why does it exist?

Because storing currency amounts in floats is a really bad idea

How to use it?

$ npm install money-math

Then

var money = require("money-math");

money.add("16.11", "17.07");        // "33.18"
money.subtract("16.00", "7.00");    // "9.00"
money.mul("24.00", "0.25");         // "6.00"
money.percent("200.00", "3.25");    // "6.50"

money.format("JPY", "236800.00");   // "236,800"
money.floatToAmount(56.345);        // "56.35"

And last, but not least :)

money.roundUpTo5Cents("42.02");     // "42.05"

Which we use for bills in CHF that are required by law to be 0 (mod 5).

License (MIT)

Copyright (c) 2012 Ivan Krechetov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

money-math's People

Contributors

ikr avatar

Stargazers

Anselmo Lima avatar

Watchers

Renato Gama avatar James Cloos avatar

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.