From 15e8d8b19b422177a89973197bd15d6565ccbc4e Mon Sep 17 00:00:00 2001 From: Hakan Ensari Date: Wed, 2 Sep 2015 22:12:42 +0100 Subject: [PATCH] Round to five significant digits Fixes #10 --- lib/quote.rb | 5 ++--- spec/quote_spec.rb | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/quote.rb b/lib/quote.rb index 6d35c56..d041567 100644 --- a/lib/quote.rb +++ b/lib/quote.rb @@ -60,13 +60,12 @@ class Quote rates end + # I'm mimicking the apparent convention of the ECB here. def round_rate(rate) if rate > 100 rate.round(2) - elsif rate > 10 - rate.round(3) else - rate.round(4) + Float("%.#{5}g" % rate) end end end diff --git a/spec/quote_spec.rb b/spec/quote_spec.rb index d0205f2..f400164 100644 --- a/spec/quote_spec.rb +++ b/spec/quote_spec.rb @@ -33,19 +33,25 @@ describe Quote do end end - describe 'when given a non-euro base' do - let(:quote) { Quote.new(base: 'USD') } + describe 'when given a custom base' do + let(:quote) { Quote.new(base: 'FOO') } it 'quotes rates against that currency' do - stub_rates 'USD' => 1.25 do |quote| - rate = quote.rates['EUR'] - rate.must_equal 0.8 + stub_rates 'FOO' => 2 do |quote| + quote.rates.keys.must_include 'EUR' end end it 'does not quote the base currency' do - stub_rates 'USD' => 1.25 do |quote| - quote.rates.keys.wont_include 'USD' + stub_rates 'FOO' => 2 do |quote| + quote.rates.keys.wont_include 'FOO' + end + end + + it 'rounds to five significant digits' do + stub_rates 'FOO' => 0.6995 do |quote| + rate = quote.rates['EUR'] + rate.must_equal 1.4296 end end end