From 08bedbe91160707cbf7d20662eb3e5a943a6a1e5 Mon Sep 17 00:00:00 2001 From: Christopher Rogers Date: Mon, 30 Mar 2015 16:02:22 -0700 Subject: [PATCH] Fixes issue with tax rounding - Instead of using a rounding method prone to floating point math issues, this uses a coercion using exp notation Signed-off-by: Christopher Rogers --- lib/recurly/pricing/calculations.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/recurly/pricing/calculations.js b/lib/recurly/pricing/calculations.js index bae2d9a8f..ed064a44e 100644 --- a/lib/recurly/pricing/calculations.js +++ b/lib/recurly/pricing/calculations.js @@ -114,8 +114,8 @@ Calculations.prototype.tax = function (done) { }); // tax estimation prefers partial cents to always round up - self.price.now.tax = Math.ceil(self.price.now.tax * 100) / 100; - self.price.next.tax = Math.ceil(self.price.next.tax * 100) / 100; + self.price.now.tax = taxCeil(self.price.now.tax); + self.price.next.tax = taxCeil(self.price.next.tax); } done.call(self); }); @@ -237,3 +237,16 @@ Calculations.prototype.planPrice = function () { function decimal (prop) { this[prop] = (Math.round(Math.max(this[prop], 0) * 100) / 100).toFixed(2); } + +/** + * Ceilings the second decimal of a number without risk of + * floating point math errors + * + * @param {Number} number + * @return {Number} + * @private + */ + +function taxCeil (number) { + return +(Math.ceil(number + 'e+2') + 'e-2'); +}