Round to five significant digits

Fixes #10
This commit is contained in:
Hakan Ensari 2015-09-02 22:12:42 +01:00
parent ddc0439340
commit 15e8d8b19b
2 changed files with 15 additions and 10 deletions

View File

@ -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

View File

@ -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