Permalink
Browse files

journal: support space as digits sep

  • Loading branch information...
ony committed Nov 2, 2017
1 parent 30e4d26 commit 47766ea9d495f370d2a6a805a02f2d6caa1fe0eb
Showing with 36 additions and 4 deletions.
  1. +6 −2 hledger-lib/Hledger/Read/Common.hs
  2. +4 −2 hledger-lib/doc/hledger_journal.5.m4.md
  3. +26 −0 tests/journal/numbers.test
@@ -24,6 +24,7 @@ import Prelude.Compat hiding (readFile)
import Control.Monad.Compat
import Control.Monad.Except (ExceptT(..), runExceptT, throwError) --, catchError)
import Control.Monad.State.Strict
import Data.Char
import Data.Data
import Data.Default
import Data.Functor.Identity
@@ -599,7 +600,7 @@ rawnumberp = do
(firstSep, groups) <- option (Nothing, []) $ do
leadingDigits <- some digitChar
option (Nothing, [leadingDigits]) . try $ do
firstSep <- oneOf sepChars
firstSep <- oneOf sepChars <|> whitespaceChar
groups <- some digitChar `sepBy1` char firstSep
return (Just firstSep, leadingDigits : groups)
@@ -614,11 +615,14 @@ rawnumberp = do
return (lastSep, fromMaybe [] digits)
-- make sure we didn't leading part of mistyped number
notFollowedBy $ oneOf sepChars
notFollowedBy $ oneOf sepChars <|> (whitespaceChar >> digitChar)
return $ dbg8 "rawnumberp" (firstSep, groups, extraGroup)
<?> "rawnumberp"
-- | Parse a unicode char that represents any non-control space char (Zs general category).
whitespaceChar :: TextParser m Char
whitespaceChar = charCategory Space
-- test_numberp = do
-- let s `is` n = assertParseEqual (parseWithState mempty numberp s) n
@@ -267,15 +267,17 @@ Amounts consist of a number and (usually) a currency symbol or commodity name. S
`3 "green apples"`\
`-$1,000,000.00`\
`INR 9,99,99,999.00`\
`EUR -2.000.000,00`
`EUR -2.000.000,00`\
`1 999 999.9455`
As you can see, the amount format is somewhat flexible:
- amounts are a number (the "quantity") and optionally a currency symbol/commodity name (the "commodity").
- the commodity is a symbol, word, or phrase, on the left or right, with or without a separating space.
If the commodity contains numbers, spaces or non-word punctuation it must be enclosed in double quotes.
- negative amounts with a commodity on the left can have the minus sign before or after it
- digit groups (thousands, or any other grouping) can be separated by commas (in which case period is used for decimal point) or periods (in which case comma is used for decimal point)
- digit groups (thousands, or any other grouping) can be separated by space or comma or period and should be used as separator between all groups
- decimal part can be separated by comma or period and should be different from digit groups separator
You can use any of these variations when recording data. However, there is some ambiguous way of representing numbers like `$1.000` and `$1,000` both may mean either one thousand or one dollar. By default hledger will assume that this is sole delimiter is used only for decimals. On the other hand commodity format declared prior to that line will help to resolve that ambiguity differently:
View
@@ -43,6 +43,32 @@ hledger bal -f -
>>>
>>>=1
# Space between digits groups
hledger bal -f - --no-total
<<<
2017/1/1
a 1 000.00 EUR
b -1 000.00 EUR
>>>
1 000.00 EUR a
-1 000.00 EUR b
>>>2
>>>=0
# Space between digits groups in commodity directive
hledger bal -f - --no-total
<<<
commodity 1 000.00 EUR
2017/1/1
a 1,000.00 EUR
b -1,000.00 EUR
>>>
1 000.00 EUR a
-1 000.00 EUR b
>>>2
>>>=0
# Default commodity
hledger bal -f -
<<<

0 comments on commit 47766ea

Please sign in to comment.