diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index 33543a1fc..79f503127 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -77,6 +77,7 @@ module Hledger.Data.Amount ( withPrecision, setFullPrecision, setNaturalPrecision, + setNaturalPrecisionUpTo, setAmountInternalPrecision, withInternalPrecision, setAmountDecimalPoint, @@ -298,6 +299,14 @@ setNaturalPrecision a = setAmountPrecision normalprecision a where normalprecision = fromIntegral $ decimalPlaces $ normalizeDecimal $ aquantity a +-- | Set an amount's display precision to just enough so that all +-- significant decimal digits will be shown, but not more than the +-- given maximum number of decimal digits. +setNaturalPrecisionUpTo :: Int -> Amount -> Amount +setNaturalPrecisionUpTo n a = setAmountPrecision (min n normalprecision) a + where + normalprecision = fromIntegral $ decimalPlaces $ normalizeDecimal $ aquantity a + -- | Get a string representation of an amount for debugging, -- appropriate to the current debug level. 9 shows maximum detail. showAmountDebug :: Amount -> String