historical: Add comments to explain the precision-finding code.

This commit is contained in:
Brett Smith 2017-07-06 10:17:08 -04:00
parent d0f5f1547c
commit f7a57ded86

View file

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