Gathering detailed insights and metrics for money-math
Gathering detailed insights and metrics for money-math
Gathering detailed insights and metrics for money-math
Gathering detailed insights and metrics for money-math
npm install money-math
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
99 Stars
126 Commits
19 Forks
8 Watching
6 Branches
7 Contributors
Updated on 12 Jul 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-15.9%
260
Compared to previous day
Last week
-9.4%
1,742
Compared to previous week
Last month
3%
7,529
Compared to previous month
Last year
90.3%
185,099
Compared to previous year
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.
1money.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:
1money.format("EUR", "-1560.00") // "-1.560,00"
Because storing currency amounts in floats is a really bad idea
Works both on Node and in the browser.
$ npm install --save money-math
1var money = require("money-math");
Download jsbn/index.js
Download money.js
1window.Money
1money.add("16.11", "17.07"); // "33.18" 2money.subtract("16.00", "7.00"); // "9.00" 3money.mul("24.00", "0.25"); // "6.00" 4money.div("64.00", "2.00"); // "32.00" 5money.percent("200.00", "3.25"); // "6.50" 6money.cmp("100.00", "200.00"); // -1 7money.isEqual("100.00", "100.00"); // true 8money.isZero("0.00"); // true 9money.isNegative("-1.00"); // true 10money.isPositive("-1.00"); // false 11money.isGreaterThan("2.00", "1.00"); // true 12money.isGreaterOrEqualTo("2.00", "2.00"); // true 13money.isLessThan("2.00", "1.00"); // false 14money.isLessrOrEqualTo("2.00", "2.00"); // true 15 16money.format("JPY", "236800.00"); // "236,800" 17money.floatToAmount(56.345); // "56.35"
And last, but not least :)
1money.roundUpTo5Cents("42.02"); // "42.05" 2money.roundTo5Cents("442.26"); // "442.25"
Which we use for bills in CHF that are required by law to be 0 (mod 5).
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:
1> 56.155.toFixed(2); 2'56.16' 3> 56.345.toFixed(2); 4'56.34'
Floats are such floats...
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 3/24 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More