From 644635b918510d64dee29adfd9446141a4b3827f Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 31 Aug 2023 03:55:29 +0100 Subject: [PATCH] imp: print: show a disambiguating decimal mark when needed Eg "1,000" (with , as a thousands separator and no decimal digits) is now displayed with a decimal mark: "1,000.". "1 000" (where space is a thousands separator) is less ambiguous, but we do the same thing (eg "1 000.") for consistency, and also to help disambiguate when forgetting to quote a numeric commodity symbol (eg "1234 0" where 1234 is a symbol that should have been in double quotes). --- hledger-lib/Hledger/Data/Amount.hs | 12 +++++++++++- hledger/test/print/print-style.test | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index 6c2e1e952..19995af15 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -517,6 +517,10 @@ showAmount = wbUnpack . showAmountB noColour -- -- * The special "missing" amount is displayed as the empty string. -- +-- * If an amount is showing digit group separators but no decimal places, +-- we force showing a decimal mark (with nothing after it) to make +-- it easier to parse correctly. +-- showAmountB :: AmountDisplayOpts -> Amount -> WideBuilder showAmountB _ Amount{acommodity="AUTO"} = mempty showAmountB opts a@Amount{astyle=style} = @@ -565,6 +569,8 @@ showAmountDebug Amount{..} = -- | Get a Text Builder for the string representation of the number part of of an amount, -- using the display settings from its commodity. Also returns the width of the number. +-- A special case: if it is showing digit group separators but no decimal places, +-- show a decimal mark (with nothing after it) to make it easier to parse correctly. showamountquantity :: Amount -> WideBuilder showamountquantity amt@Amount{astyle=AmountStyle{asdecimalmark=mdec, asdigitgroups=mgrps}} = signB <> intB <> fracB @@ -578,10 +584,14 @@ showamountquantity amt@Amount{astyle=AmountStyle{asdecimalmark=mdec, asdigitgrou (intPart, fracPart) = T.splitAt intLen numtxtwithzero intB = applyDigitGroupStyle mgrps intLen $ if decplaces == 0 then numtxt else intPart signB = if mantissa < 0 then WideBuilder (TB.singleton '-') 1 else mempty - fracB = if decplaces > 0 + fracB = if decplaces > 0 || isshowingdigitgroupseparator then WideBuilder (TB.singleton dec <> TB.fromText fracPart) (1 + fromIntegral decplaces) else mempty + isshowingdigitgroupseparator = case mgrps of + Just (DigitGroups _ (rightmostgrplen:_)) -> intLen > fromIntegral rightmostgrplen + _ -> False + -- | Given an integer as text, and its length, apply the given DigitGroupStyle, -- inserting digit group separators between digit groups where appropriate. -- Returns a Text builder and the number of digit group separators used. diff --git a/hledger/test/print/print-style.test b/hledger/test/print/print-style.test index c10923135..d76dcb114 100644 --- a/hledger/test/print/print-style.test +++ b/hledger/test/print/print-style.test @@ -73,3 +73,17 @@ $ hledger -f- print # (a) A1.234 @ B3.456 = A1.234 @ B3.456 # # >= + +# 3. When showing digit group marks, print always shows a decimal mark as well, +# even when no decimal digits are shown. +< +decimal-mark . +2023-01-01 + (a) 1,000 + +$ hledger -f- print +2023-01-01 + (a) 1,000. + +>= +