lib: setNaturalPrecisionUpTo

This commit is contained in:
Simon Michael 2019-06-14 18:32:45 -07:00
parent 5cb334514c
commit 2da50875ee

View File

@ -77,6 +77,7 @@ module Hledger.Data.Amount (
withPrecision, withPrecision,
setFullPrecision, setFullPrecision,
setNaturalPrecision, setNaturalPrecision,
setNaturalPrecisionUpTo,
setAmountInternalPrecision, setAmountInternalPrecision,
withInternalPrecision, withInternalPrecision,
setAmountDecimalPoint, setAmountDecimalPoint,
@ -298,6 +299,14 @@ setNaturalPrecision a = setAmountPrecision normalprecision a
where where
normalprecision = fromIntegral $ decimalPlaces $ normalizeDecimal $ aquantity a 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, -- | Get a string representation of an amount for debugging,
-- appropriate to the current debug level. 9 shows maximum detail. -- appropriate to the current debug level. 9 shows maximum detail.
showAmountDebug :: Amount -> String showAmountDebug :: Amount -> String