From 9e4b302af7413e62c8d5949fe77578e235d0da9f Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 16 Jan 2019 14:45:50 -0800 Subject: [PATCH] lib: setFullPrecision, setMinimalPrecision (#941) --- hledger-lib/Hledger/Data/Amount.hs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index 55eb0a05f..2906c8549 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -75,6 +75,8 @@ module Hledger.Data.Amount ( maxprecisionwithpoint, setAmountPrecision, withPrecision, + setFullPrecision, + setMinimalPrecision, setAmountInternalPrecision, withInternalPrecision, setAmountDecimalPoint, @@ -124,7 +126,7 @@ module Hledger.Data.Amount ( ) where import Data.Char (isDigit) -import Data.Decimal (roundTo) +import Data.Decimal (roundTo, decimalPlaces, normalizeDecimal) import Data.Function (on) import Data.List import Data.Map (findWithDefault) @@ -278,6 +280,23 @@ setAmountPrecision p a@Amount{astyle=s} = a{astyle=s{asprecision=p}} withPrecision :: Amount -> Int -> Amount withPrecision = flip setAmountPrecision +-- | Increase an amount's display precision, if necessary, enough so +-- that it will be shown exactly, with all significant decimal places +-- (excluding trailing zeros). +setFullPrecision :: Amount -> Amount +setFullPrecision a = setAmountPrecision p a + where + p = max displayprecision normalprecision + displayprecision = asprecision $ astyle a + normalprecision = fromIntegral $ decimalPlaces $ normalizeDecimal $ aquantity a + +-- | Set an amount's display precision to just enough so that it will +-- be shown exactly, with all significant decimal places. +setMinimalPrecision :: Amount -> Amount +setMinimalPrecision a = setAmountPrecision 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