frankfurter/lib/roundable.rb
BadMark d0a43e357a
Some checks failed
CI/CD Pipeline / test (push) Has been cancelled
CI/CD Pipeline / publish (push) Has been cancelled
CI/CD Pipeline / release (push) Has been cancelled
up
2026-05-21 01:38:14 -06:00

27 lines
919 B
Ruby

# frozen_string_literal: true
# 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.
#
# https://en.wikipedia.org/wiki/Exchange_rate#Quotations
module Roundable
def round(value)
if value > 5000
value.round
elsif value > 80
Float(format("%<value>.2f", value:))
elsif value > 20
Float(format("%<value>.3f", value:))
elsif value > 1
Float(format("%<value>.4f", value:))
elsif value > 0.0001
Float(format("%<value>.5f", value:))
else
Float(format("%<value>.6f", value:))
end
end
end