From f7a57ded868cda1b1e6c7d280190f8b08df5963b 2017-07-06 14:17:08 From: Brett Smith Date: 2017-07-06 14:17:08 Subject: [PATCH] historical: Add comments to explain the precision-finding code. --- diff --git a/oxrlib/commands/historical.py b/oxrlib/commands/historical.py index bf04e7bc03c735d412efb2208e98ca17917da2ab..3223f943185055add9bee5c429a84fc3afddc8d3 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))