From 2da50875eed5a9043f080902fa4e0cd31d2bbc0c Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 14 Jun 2019 18:32:45 -0700 Subject: [PATCH] lib: setNaturalPrecisionUpTo --- hledger-lib/Hledger/Data/Amount.hs | 9 +++++++++ 1 file changed, 9 insertions(+) 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