From b2f439eb11105cca513f1f0b5e0528277faed28b Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 20 Jan 2011 00:17:32 +0000 Subject: [PATCH] try showing integral "maxprecision" amounts without the .0 (except in add's defaults, where it helps) --- hledger-lib/Hledger/Data/Amount.hs | 16 ++++++++++++---- hledger/Hledger/Cli/Add.hs | 6 ++++-- tests/add.test | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index 2bc44515c..061df8684 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -53,6 +53,7 @@ module Hledger.Data.Amount ( isReallyZeroMixedAmountCost, isZeroMixedAmount, maxprecision, + maxprecisionwithpoint, missingamt, normaliseMixedAmount, nullamt, @@ -183,13 +184,20 @@ showamountquantity :: Amount -> String showamountquantity (Amount (Commodity {decimalpoint=d,precision=p,separator=s,separatorpositions=spos}) q _) = punctuatenumber d s spos $ qstr where - qstr -- | p == maxprecision && isint q = printf "%d" (round q::Integer) - | p == maxprecision = printf "%f" q - | otherwise = printf ("%."++show p++"f") q -- isint n = fromIntegral (round n) == n + qstr -- | p == maxprecision && isint q = printf "%d" (round q::Integer) + | p == maxprecisionwithpoint = printf "%f" q + | p == maxprecision = chopdotzero $ printf "%f" q + | otherwise = printf ("%."++show p++"f") q + +chopdotzero str = reverse $ case reverse str of + '0':'.':s -> s + s -> s -- | A special precision value meaning show all available digits. -maxprecision = 999999 +maxprecision = 999998 +-- | Similar, forces display of a decimal point. +maxprecisionwithpoint = 999999 -- | Replace a number string's decimal point with the specified character, -- and add the specified digit group separators. diff --git a/hledger/Hledger/Cli/Add.hs b/hledger/Hledger/Cli/Add.hs index 50dd69644..8ea9ab8eb 100644 --- a/hledger/Hledger/Cli/Add.hs +++ b/hledger/Hledger/Cli/Add.hs @@ -122,8 +122,10 @@ getPostings ctx accept historicalps enteredps = do | n > 1 = Just balancingamountstr | otherwise = Nothing where - historicalamountstr = showMixedAmountWithPrecision maxprecision $ pamount $ fromJust bestmatch' - balancingamountstr = showMixedAmountWithPrecision maxprecision $ negate $ sumMixedAmountsPreservingHighestPrecision $ map pamount enteredrealps + -- force a decimal point in the output in case there's a + -- digit group separator that would be mistaken for one + historicalamountstr = showMixedAmountWithPrecision maxprecisionwithpoint $ pamount $ fromJust bestmatch' + balancingamountstr = showMixedAmountWithPrecision maxprecisionwithpoint $ negate $ sumMixedAmountsPreservingHighestPrecision $ map pamount enteredrealps amountstr <- askFor (printf "amount %d" n) defaultamountstr validateamount let amount = fromparse $ runParser (someamount <|> return missingamt) ctx "" amountstr amount' = fromparse $ runParser (someamount <|> return missingamt) nullctx "" amountstr diff --git a/tests/add.test b/tests/add.test index 87e14a00c..bfb7990cd 100644 --- a/tests/add.test +++ b/tests/add.test @@ -86,7 +86,7 @@ a b . ->>> /a +£1,000.0/ +>>> /a +£1,000.00/ # 9. default amounts should not fail to balance due to precision bin/hledger -f nosuch.journal add <<<