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).
This commit is contained in:
Simon Michael 2023-08-31 03:55:29 +01:00
parent f620a3e0ea
commit 644635b918
2 changed files with 25 additions and 1 deletions

View File

@ -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.

View File

@ -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.
>=