From 05f3c690d4c9b9fc402079f0042a533c6f3f98db Mon Sep 17 00:00:00 2001 From: Ben Fiedler Date: Mon, 18 Jul 2022 08:48:47 +0200 Subject: [PATCH] imp: Add ' as possible digit separator It is used for Swiss Francs: CHF 1'000. --- hledger-lib/Hledger/Read/Common.hs | 4 +++- hledger/hledger.m4.md | 6 ++++-- hledger/test/journal/numbers.test | 13 +++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index b20faac7f5e8..f3a526d42b8d 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -1077,6 +1077,8 @@ disambiguateNumber msuggestedStyle (AmbiguousNumber grp1 sep grp2) = -- Left (AmbiguousNumber "1" ',' "000") -- >>> parseTest rawnumberp "1 000" -- Right (WithSeparators ' ' ["1","000"] Nothing) +-- >>> parseTest rawnumberp "1'000" +-- Right (WithSeparators '\'' ["1","000"] Nothing) -- rawnumberp :: TextParser m (Either AmbiguousNumber RawNumber) rawnumberp = label "number" $ do @@ -1142,7 +1144,7 @@ rawnumberp = label "number" $ do pure $ NoSeparators grp1 (Just (decPt, mempty)) isDigitSeparatorChar :: Char -> Bool -isDigitSeparatorChar c = isDecimalMark c || c == ' ' +isDigitSeparatorChar c = isDecimalMark c || c == ' ' || c == '\'' -- | Some kinds of number literal we might parse. data RawNumber diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 49c4f3386002..05b3cb27167b 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -1184,10 +1184,11 @@ A *decimal mark* can be written as a period or a comma: In the integer part of the quantity (left of the decimal mark), groups of digits can optionally be separated by a *digit group mark* - a -space, comma, or period (different from the decimal mark): +space, comma, single quote, or period (different from the decimal mark): $1,000,000.00 EUR 2.000.000,00 + CHF 2'000'000.00 INR 9,99,99,999.00 1 000 000.9455 @@ -2192,9 +2193,10 @@ by 0 or more decimal digits. A few more examples: ```journal -# number formats for $, EUR, INR and the no-symbol commodity: +# number formats for $, EUR, CHF, INR and the no-symbol commodity: commodity $1,000.00 commodity EUR 1.000,00 +commodity CHF 1'000.00 commodity INR 9,99,99,999.0 commodity 1 000 000. ``` diff --git a/hledger/test/journal/numbers.test b/hledger/test/journal/numbers.test index 1f68ea968509..f0bab7c316ab 100644 --- a/hledger/test/journal/numbers.test +++ b/hledger/test/journal/numbers.test @@ -234,3 +234,16 @@ Balance changes in 2021: ===++=================================== a || -0.12345678901234567890123456 EUR >>>=0 + +# 19. number begins with a separator char +hledger bal -f - +<<< +commodity 1'000.00 CHF + +2017/1/1 + a 1000 CHF + b -1000 CHF +>>> + 1'000.00 CHF a + -1'000.00 CHF b +>>>=0