• Stars
    star
    101
  • Rank 338,166 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 12 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

jsbn-based arbitrary precision operations on currency amounts "XXX.YY"; because floats are BAD for representing money

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 the following currencies:

  • CHF
  • EUR
  • GBP
  • JPY
  • LTL
  • PLN
  • SEK
  • SKK
  • UAH
  • USD
money.format("EUR", "-1560.00") // "-1.560,00"

Why does it exist?

Because storing currency amounts in floats is a really bad idea

Install

Works both on Node and in the browser.

Node/Browserify/Webpack

$ npm install --save money-math
var money = require("money-math");

Browser global

Download jsbn/index.js

Download money.js

window.Money

Usage

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.div("64.00", "2.00");         // "32.00"
money.percent("200.00", "3.25");    // "6.50"
money.cmp("100.00", "200.00");      // -1
money.isEqual("100.00", "100.00");  // true
money.isZero("0.00");               // true
money.isNegative("-1.00");          // true
money.isPositive("-1.00");          // false
money.isGreaterThan("2.00", "1.00"); // true
money.isGreaterOrEqualTo("2.00", "2.00"); // true
money.isLessThan("2.00", "1.00"); // false
money.isLessrOrEqualTo("2.00", "2.00"); // true

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

And last, but not least :)

money.roundUpTo5Cents("42.02");     // "42.05"
money.roundTo5Cents("442.26");      // "442.25"

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

An important note on the amount "data type"

The amount strings are expected to strictly adhere to the format described by the regular expression noted above. Thus, for example, it must be:

  • "10.10", not "10.1", not "10.100";
  • "10.00", not 10, not "10", not "10.0".

That's a precondition for any of the API functions accepting amount arguments to work correctly. I understand that it may be confusing to some of new users; but I believe that's an optimally pragmatic way to mimic, by convention, an algebraic data type in idiomatic JavaScript -- a (very) dynamically typed language.

Just for the sake of convenience, we provide a way to approximate an imprecise float value in the amounts field with money.floatToAmount(โ€ฆ) (half up rounding applied). Once all the values are amounts, money-math guarantees that all the field operations keep the results withing the field. Classic algebra.

A thoughtful reader might ask, why have money.floatToAmount(), when there's the Number.prototype.toFixed(2)? Well, because:

> 56.155.toFixed(2);
'56.16'
> 56.345.toFixed(2);
'56.34'

Floats are such floats...

More Repositories

1

react-star-rating-input

React.js components for entering 0โ€”N stars (N is 5 by default), or displaying 0โ€”N stars
TypeScript
53
star
2

tv4-formats

Common JSON Schema string format constraints in the form of tv4 validator callbacks
JavaScript
30
star
3

money-math-php

gmp-based arbitrary precision operations on currency amounts "XXX.YY"; because floats are BAD for representing money
PHP
30
star
4

500px-slideshow

A bookmarklet starting a slide show for any 500px.com photo listing
JavaScript
17
star
5

normalize-for-search

Un-accents and un-umlauts characters in a string. Also preliminary converts the string to lower case. We use it for autocomplete: both for the matched strings -- on the server side, when indexing; and for the strings the user types into a text input in the browser.
JavaScript
14
star
6

react-navbar

React.js component rendering a translatable menu bar with Twitter Bootstrap Navbar HTML markup
TypeScript
12
star
7

react-period-of-stay-input

React.js component for entering a period of stay in a hotel: check-in + check-out date
TypeScript
9
star
8

jsdom-test-browser

Boilerplate I use to set up React.js components testing harness under Node.js, based on jsdom
JavaScript
9
star
9

estimates-template

Write user stories in markdown, list the implementation tasks for each (with an estimation range), publish an interactive estimation document
JavaScript
8
star
10

estimate-tasks

Write and estimate user stories, properly summing up the estimates as random values
JavaScript
6
star
11

annotator-store-lite

A backend store for the Annotator โ€“ based on PHP/Silex and SQLite
PHP
5
star
12

street-magic-with-functions

That's the talk I gave at JSZurich on 22.02.2011
JavaScript
5
star
13

react-hotels-on-map

Presents a set of hotels as clustered markers on Google Map, each with an info window displaying the hotel summary
JavaScript
4
star
14

algo-practice

My solutions of various algo problems
C++
4
star
15

rise-against-the-state

Zurich Frontend Conference 2012 lightning talk
JavaScript
3
star
16

react-room-occupancy-input

React.js component for specifying a hotel room occupancy: how many adults, how many kids, how old kids are
JavaScript
3
star
17

cli-utils

My personal CLI utilities I need to use often on different hosts
Shell
2
star
18

couch-js-devkit

JavaScript
2
star
19

translator-couch

CouchDB schema for Magomogo/translator-utils, Magomogo/translator, and ikr/translator-node
JavaScript
2
star
20

bform-test-case

A mini-project demonstating a problem with custom templates in https://github.com/powmedia/backbone-forms
JavaScript
1
star
21

magtfp

The code I write while reading @drboolean's book
JavaScript
1
star
22

pomodoro-logger

A little tool I use to log my Pomodoro-s
JavaScript
1
star
23

CyrTranslit

Miranda IM plugin, applying the Cyrillic to Latin transliteration for the sent instant messages
C++
1
star
24

json-schema-for-http-apis

My lightning talk at the joint meet-up of JSZurich and Webtuesday on 14.01.2014
1
star
25

skiena

The code I'm writing while reading S. Skiena's Algorithms Design Manual book
JavaScript
1
star
26

fyrfyrfyr

Basic tools for composing functions, to write simpler and safer programs
PHP
1
star
27

annotator-bower

https://github.com/okfn/annotator packaged for installation via bower package manager
JavaScript
1
star
28

7sins

Slides for a lightning talk at WebTuesday Zรผrich, 14.06.2011
1
star