@@ -48,7 +48,8 @@ import Hledger.Data.Types
4848import Hledger.Data.Amount
4949import Hledger.Data.Dates (nulldate )
5050import Text.Printf (printf )
51- import Data.Decimal (decimalPlaces , roundTo )
51+ import Data.Decimal (decimalPlaces , roundTo , Decimal )
52+ import Data.Word (Word8 )
5253
5354
5455------------------------------------------------------------------------------
@@ -290,12 +291,17 @@ priceLookup makepricegraph d from mto =
290291 -- aggregate all the prices into one
291292 product rates
292293 -- product (Decimal's Num instance) normalises, stripping trailing zeros.
293- -- Here we undo that (by restoring the old max precision with roundTo),
294- -- so that amountValueAtDate can see the original internal precision,
295- -- to use as the display precision of calculated value amounts.
296- -- (This can add more than the original number of trailing zeros to some prices,
297- -- making them seem more precise than they were, but it seems harmless here.)
298- & roundTo (maximum $ map decimalPlaces rates)
294+ -- But we want to preserve even those, since the number of decimal digits
295+ -- here will guide amountValueAtDate in setting the Amount display precision later.
296+ -- So we restore them. Or rather, we ensure as many decimal digits as the maximum seen among rates.
297+ -- (Some prices might end up more precise than they were, but that seems harmless here.)
298+ & setMinDecimalPlaces (maximum $ map decimalPlaces rates)
299+
300+ -- Ensure this Decimal has at least this many decimal places, adding trailing zeros if necessary.
301+ setMinDecimalPlaces :: Word8 -> Decimal -> Decimal
302+ setMinDecimalPlaces n d
303+ | decimalPlaces d < n = roundTo n d -- too few, add some zeros
304+ | otherwise = d -- more than enough, keep as-is
299305
300306tests_priceLookup =
301307 let
0 commit comments