From dd61034908c7ea50f426af4af6376a32cb7573a1 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 13 Jan 2015 14:47:13 +1100 Subject: [PATCH] Fix fractional cents appearing on sales tax report totals --- lib/open_food_network/sales_tax_report.rb | 4 ++++ spec/lib/open_food_network/sales_tax_report_spec.rb | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/open_food_network/sales_tax_report.rb b/lib/open_food_network/sales_tax_report.rb index 3920773d06..d5b69011d2 100644 --- a/lib/open_food_network/sales_tax_report.rb +++ b/lib/open_food_network/sales_tax_report.rb @@ -42,6 +42,10 @@ module OpenFoodNetwork end end + totals.each_pair do |k, v| + totals[k] = totals[k].round(2) + end + totals end diff --git a/spec/lib/open_food_network/sales_tax_report_spec.rb b/spec/lib/open_food_network/sales_tax_report_spec.rb index cf00f650ff..043eac6ae7 100644 --- a/spec/lib/open_food_network/sales_tax_report_spec.rb +++ b/spec/lib/open_food_network/sales_tax_report_spec.rb @@ -21,6 +21,15 @@ module OpenFoodNetwork totals[:items_total].should == 36 end + context "when floating point math would result in fractional cents" do + let(:li1) { double(:line_item, quantity: 1, amount: 0.11) } + let(:li2) { double(:line_item, quantity: 2, amount: 0.12) } + + it "rounds to the nearest cent" do + totals[:items_total].should == 0.23 + end + end + it "calculates the taxable total price" do totals[:taxable_total].should == 36 end