Skip to content

Commit

Permalink
Fixes rounding issues around tax and floats
Browse files Browse the repository at this point in the history
- tax amounts are truncated to 6 decimals before being added to totals
- same for discounts
- fixes #147

Signed-off-by: Christopher Rogers <[email protected]>
  • Loading branch information
chrissrogers committed Sep 8, 2014
1 parent 25c505b commit 30a7310
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/recurly/pricing/calculations.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ Calculations.prototype.tax = function (done) {
} else {
each(taxes, function (tax) {
if (tax.type === 'usst' && self.items.plan.tax_exempt) return;
self.price.now.tax += self.price.now.subtotal * tax.rate;
self.price.next.tax += self.price.next.subtotal * tax.rate;
self.price.now.tax += parseFloat((self.price.now.subtotal * tax.rate).toFixed(6));
self.price.next.tax += parseFloat((self.price.next.subtotal * tax.rate).toFixed(6));
});

// tax estimation prefers partial cents to always round up
Expand Down Expand Up @@ -156,8 +156,10 @@ Calculations.prototype.discount = function () {

if (coupon) {
if (coupon.discount.rate) {
this.price.now.discount = Math.round(this.price.now.subtotal * coupon.discount.rate * 100) / 100;
this.price.next.discount = Math.round(this.price.next.subtotal * coupon.discount.rate * 100) / 100;
var discountNow = parseFloat((this.price.now.subtotal * coupon.discount.rate).toFixed(6));
var discountNext = parseFloat((this.price.next.subtotal * coupon.discount.rate).toFixed(6));
this.price.now.discount = Math.round(discountNow * 100) / 100;
this.price.next.discount = Math.round(discountNext * 100) / 100;
} else {
this.price.now.discount = coupon.discount.amount[this.items.currency];
this.price.next.discount = coupon.discount.amount[this.items.currency];
Expand Down

0 comments on commit 30a7310

Please sign in to comment.