From f7a57ded868cda1b1e6c7d280190f8b08df5963b Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Thu, 6 Jul 2017 10:17:08 -0400 Subject: [PATCH] historical: Add comments to explain the precision-finding code. --- oxrlib/commands/historical.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/oxrlib/commands/historical.py b/oxrlib/commands/historical.py index bf04e7b..3223f94 100644 --- a/oxrlib/commands/historical.py +++ b/oxrlib/commands/historical.py @@ -109,10 +109,15 @@ class LedgerFormatter(Formatter): if denomination is None: return amt_s full_rate = self.rate.convert(1, currency, denomination) + # Starting from self.rate_prec, find the least amount of precision to + # make sure the `from` amount converts exactly to the `to` amount. to_amt = self.currency_decimal(amount * full_rate, denomination) for prec in itertools.count(self.rate_prec): rate = self.normalize_rate(full_rate, prec) got_amt = self.currency_decimal(amount * rate, denomination) + # If got_amt == to_amt, this is enough precision to do the + # conversion exactly, so we're done. + # If rate == full_rate, there's no more precision available, so stop. if (got_amt == to_amt) or (rate == full_rate): break return "{} {}".format(amt_s, self.format_ledger_rate_raw(rate, denomination))