Permalink
Browse files

Add money.roundUpTo5Cents()

  • Loading branch information...
1 parent 344fb80 commit f826239353a11863c6aeaad1f6dc70ee1b16f240 @ikr committed Oct 17, 2012
Showing with 33 additions and 2 deletions.
  1. +7 −1 README.md
  2. +11 −0 money.js
  3. +1 −1 package.json
  4. +14 −0 spec/money.spec.js
View
@@ -32,7 +32,7 @@ Because storing currency amounts in floats [is a really bad idea](http://stackov
$ npm install money-math
-And then
+Then
var money = require("money-math");
@@ -44,6 +44,12 @@ And then
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
View
@@ -120,4 +120,15 @@
(r.gt("4999") ? q.add(1) : q).toString()
);
};
+
+ exports.roundUpTo5Cents = function (amount) {
+ var lastDigit = parseInt(amount.substr(-1), 10),
+ additon = "0.00";
+
+ if ((lastDigit % 5) !== 0) {
+ additon = "0.0" + (5 - (lastDigit % 5));
+ }
+
+ return exports.add(amount, additon);
+ };
}(exports));
View
@@ -1,6 +1,6 @@
{
"name": "money-math",
- "version": "0.2.1",
+ "version": "0.3.0",
"description": "bignum-based arbitrary precision operations on currency amounts \"XXX.YY\"; because floats are BAD for representing money",
"main": "money.js",
View
@@ -207,4 +207,18 @@
expect(money.floatToAmount(-0.346)).toBe("-0.35");
});
});
+
+ describe("money.roundUpTo5Cents()", function () {
+ it("is identical for zero", function () {
+ expect(money.roundUpTo5Cents("0.00")).toBe("0.00");
+ });
+
+ it("rounds 02 to 05", function () {
+ expect(money.roundUpTo5Cents("1.02")).toBe("1.05");
+ });
+
+ it("rounds 06 to 10", function () {
+ expect(money.roundUpTo5Cents("156.06")).toBe("156.10");
+ });
+ });
}());

0 comments on commit f826239

Please sign in to comment.