2018-07-05 21:19:37 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Roundable
|
|
|
|
# To paraphrase Wikipedia, most currency pairs are quoted to four decimal
|
|
|
|
# places. An exception to this is exchange rates with a value of less than
|
|
|
|
# 1.000, which are quoted to five or six decimal places. Exchange rates
|
|
|
|
# greater than around 20 are usually quoted to three decimal places and
|
|
|
|
# exchange rates greater than 80 are quoted to two decimal places.
|
|
|
|
# Currencies over 5000 are usually quoted with no decimal places.
|
2020-05-02 16:21:58 +02:00
|
|
|
#
|
|
|
|
# https://en.wikipedia.org/wiki/Exchange_rate#Quotations
|
2018-07-05 21:19:37 +02:00
|
|
|
def round(value)
|
|
|
|
if value > 5000
|
|
|
|
value.round
|
|
|
|
elsif value > 80
|
2023-02-28 17:49:48 +01:00
|
|
|
Float(format('%<value>.2f', value:))
|
2018-07-05 21:19:37 +02:00
|
|
|
elsif value > 20
|
2023-02-28 17:49:48 +01:00
|
|
|
Float(format('%<value>.3f', value:))
|
2018-07-05 21:19:37 +02:00
|
|
|
elsif value > 1
|
2023-02-28 17:49:48 +01:00
|
|
|
Float(format('%<value>.4f', value:))
|
2020-05-02 16:21:58 +02:00
|
|
|
# I had originally opted to round smaller numbers simply to five decimal
|
|
|
|
# places but introduced this refinement to handle an edge case where a
|
|
|
|
# lower-rate base currency like IDR produces less precise quotes.
|
|
|
|
elsif value > 0.0001
|
2023-02-28 17:49:48 +01:00
|
|
|
Float(format('%<value>.5f', value:))
|
2020-05-02 16:21:58 +02:00
|
|
|
else
|
2023-02-28 17:49:48 +01:00
|
|
|
Float(format('%<value>.6f', value:))
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|