Skip to content

Commit

Permalink
Fixes issue with tax rounding
Browse files Browse the repository at this point in the history
- Instead of using a rounding method prone to floating point math issues, this uses a coercion using exp notation

Signed-off-by: Christopher Rogers <chrissrogers@gmail.com>
  • Loading branch information
chrissrogers committed Mar 30, 2015
1 parent 30f8c55 commit cff37da
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/recurly/pricing/calculations.js
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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');
}

0 comments on commit cff37da

Please sign in to comment.