You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistent rounding for balance CSV rules (due to amount1 0) #1465
However it is very unexpected that one commodity has no decimals guessed, and the other one has 4
(and this will lead to a balance assertion failure further down the line because the 21.058 is the correct value).
There is no way to specify a 'commodity' in a rules file, so have to rely on hledger guessing.
If I modify amount1 0 to amount1 0.000 then I get the correct result, this was very unexpected. Perhaps hledger should print a warning if it ever does truncation on a line that has values with larger number of decimals?
FWIW -O json has the correct amount printed.
I only added amount1 0 because without it balance assertions wouldn't work.
Thanks for the report. I think the same thing happens in latest hledger.
Minimal examples are good, but I'm finding this one a little hard to debug - could you provide one that's a little more realistic ? Eg one record, but the full record and rules you're using ?
My CSV files have 2 kinds of entries: transactions without balance, and balances.
Balances I want to translate to balance assertions, and for that the only way I found it to work with hledger was to hardcode the amount to 0 in the rules file (otherwise hledger complains:
real postings' sum should be 0 but is: $21.058
2018-12-31
expenses:unknown $21.058 = $21.05
). If there is a better way, please let me know.
Here is a more realistic CSV where it prints '21':
If hledger's rules file would accept a commodity declaration then perhaps the guessing could be avoided? (i.e. I would declare that MYSTOCK has 3 decimal places), but even when it is guessing perhaps it should guess independently for the amount and the balance? (the balance has no decimal places, fine, but the balance has 3 decimal places, throwing it away looses information).
@edwintorok, thanks. I will read in more detail when I can. But in case this moves us forward at all: does the following example (possibly tweaked further) capture (one of) the issues ? (I ignored the price field, thinking it may not be essential.)
Possible interim workaround: in the amount1 rule, either add a commodity symbol (any), or add the maximum number of decimal places seen in balance amounts ?
Setting the posting amount to 0 with no commodity symbol or decimal places also hides the balance amount's decimal places
Assigning a literal amount value in rules sets a commodity style which can interfere in unwanted ways with other amounts, eg hiding decimal places in a balance assertion amount.
Thanks, perhaps it is worth documenting this at least in the CSV import page, to say that for balance assertions you need to provide an amount of 0 with same commodity symbol, or with the desired number of decimal places.
Not sure whether there is anything hledger itself could do better, perhaps warn if an amount of 0 is used in a rules file, since it is likely not what the user intended?
Thanks, perhaps it is worth documenting this at least in the CSV import page, to say that for balance assertions you need to provide an amount of 0 with same commodity symbol, or with the desired number of decimal places.
Currently, hledger print -x produces lines that say 0 = $20.25 for balance assertions: without a commodity symbol and without decimals. So I don't think that's a mistake in the docs.
Contrary to what I said above, today I think the a basic issue here is that balance assertion amounts get their precision limited by the commodity display style, like all other amounts. Which seems to contradict https://hledger.org/hledger.html#commodity-display-style, which says "For the amounts in each commodity, hledger chooses a consistent display style. (Except for price amounts, which are always displayed as written)."
The print command does not seem to implement that. [Correction: I misread. Price amounts indeed were not rounded.]
Here's another example using just a journal file:
# display A with no decimal places:
commodity A 1.
2021-01-01
(a) 0.001A
(a) 1.122A = 1.123A
$ hledger print
2021-01-01
(a) 0
(a) A 1 = A 1
If I make print preserve the full precision of assertion/assignment amounts, it looks like this:
$ hledger print
2021-01-01
(a) 0
(a) A 1 = A 1.123
Which I guess is an improvement, and makes it doc-compliant. [Fixed in master.]
The entry still looks weird of course - though it is actually balanced, it doesn't look that way when displayed with the requested commodity display style. And hledger won't be able to re-parse this entry. That is issue #931.
However it is very unexpected that one commodity has no decimals guessed, and the other one has 4
This is normal hledger behaviour, at least. Each commodity has its own display style. The amount1 and quantity amounts are in the "no-symbol" commodity, while the price is in the $ commodity, and their styles are inferred separately.
Secondly: for the no-symbol commodity it has inferred zero decimal places, based on only the 0 amount in the amount1 0 rule I guess. But we would expect it to also consider the amounts in the CSV, eg the 21.058, and therefore to infer three decimal places for the no-symbol commodity.
However.. numbers in the CSV don't necessarily have a commodity symbol attached. Sometimes it's in another field, sometimes we add it in the rules. So in general it's a little unclear which CSV amounts should be used to infer the number of decimal places for which commodities. Any thoughts ? [And consider that CSV can contain many number fields, with different numbers of decimal places.]
That said.. in the usual case where you're using the CSV amount as a posting amount, all the decimals are copied to the journal entry and then the display style is inferred as you'd expect.
Here the trouble arises because 1. the posting amount is hard coded to 0 with no decimals and 2. there are no other records being processed, which if some of them used the CSV amount as posting amount would cause the display style to use more decimal places. Eg:
It would be better for print to always show all decimal places. In master, now it does.
For other commands: the number of decimal places displayed is influenced by the decimal places in the rule-generated journal amounts, but not by the original amounts in the CSV. This has been clarified in the CSV Tips doc.
Activity
simonmichael commentedon Jan 17, 2021
Thanks for the report. I think the same thing happens in latest hledger.
Minimal examples are good, but I'm finding this one a little hard to debug - could you provide one that's a little more realistic ? Eg one record, but the full record and rules you're using ?
simonmichael commentedon Jan 17, 2021
Or maybe this is realistic ? Are you actually hard coding the amount to 0 ?
edwintorok commentedon Jan 17, 2021
My CSV files have 2 kinds of entries: transactions without balance, and balances.
Balances I want to translate to balance assertions, and for that the only way I found it to work with hledger was to hardcode the amount to 0 in the rules file (otherwise hledger complains:
). If there is a better way, please let me know.
Here is a more realistic CSV where it prints '21':
And here is another more detailed CSV that prints 21.058:
(The CSV files are converted by a script of mine from Fidelity's PDF statements, and there are statements for various periods,
that sometimes overlap)
and more realistic rules (they're a bit hacked together, I'm in the process of refining them):
edwintorok commentedon Jan 17, 2021
If hledger's rules file would accept a
commoditydeclaration then perhaps the guessing could be avoided? (i.e. I would declare that MYSTOCK has 3 decimal places), but even when it is guessing perhaps it should guess independently for the amount and the balance? (the balance has no decimal places, fine, but the balance has 3 decimal places, throwing it away looses information).simonmichael commentedon Jan 18, 2021
@edwintorok, thanks. I will read in more detail when I can. But in case this moves us forward at all: does the following example (possibly tweaked further) capture (one of) the issues ? (I ignored the price field, thinking it may not be essential.)
simonmichael commentedon Jan 18, 2021
Or better:
Setting the posting amount to 0 with no commodity symbol or decimal places also hides the balance amount's decimal places (which is bad):
It doesn't happen if the posting amount has a commodity symbol:
simonmichael commentedon Jan 18, 2021
Possible interim workaround: in the amount1 rule, either add a commodity symbol (any), or add the maximum number of decimal places seen in balance amounts ?
simonmichael commentedon Jan 18, 2021
PS and yes you are doing the right thing by setting amount to 0 to create a standalone balance assertion.
simonmichael commentedon Jan 18, 2021
Assigning a literal amount value in rules sets a commodity style which can interfere in unwanted ways with other amounts, eg hiding decimal places in a balance assertion amount.
edwintorok commentedon Jan 18, 2021
Thanks, perhaps it is worth documenting this at least in the CSV import page, to say that for balance assertions you need to provide an amount of 0 with same commodity symbol, or with the desired number of decimal places.
Not sure whether there is anything
hledgeritself could do better, perhaps warn if an amount of0is used in a rules file, since it is likely not what the user intended?simonmichael commentedon Jan 18, 2021
the-solipsist commentedon Jan 26, 2021
Currently,
hledger print -xproduces lines that say0 = $20.25for balance assertions: without a commodity symbol and without decimals. So I don't think that's a mistake in the docs.simonmichael commentedon Feb 4, 2021
Contrary to what I said above, today I think
thea basic issue here is that balance assertion amounts get their precision limited by the commodity display style, like all other amounts. Which seems to contradict https://hledger.org/hledger.html#commodity-display-style, which says "For the amounts in each commodity, hledger chooses a consistent display style. (Except for price amounts, which are always displayed as written)."The print command does not seem to implement that. [Correction: I misread. Price amounts indeed were not rounded.]
Here's another example using just a journal file:
If I make print preserve the full precision of assertion/assignment amounts, it looks like this:
Which I guess is an improvement, and makes it doc-compliant. [Fixed in master.]
The entry still looks weird of course - though it is actually balanced, it doesn't look that way when displayed with the requested commodity display style. And hledger won't be able to re-parse this entry. That is issue #931.
simonmichael commentedon Feb 4, 2021
Two more issues:
This is normal hledger behaviour, at least. Each commodity has its own display style. The amount1 and quantity amounts are in the "no-symbol" commodity, while the price is in the $ commodity, and their styles are inferred separately.
Secondly: for the no-symbol commodity it has inferred zero decimal places, based on only the
0amount in theamount1 0rule I guess. But we would expect it to also consider the amounts in the CSV, eg the21.058, and therefore to infer three decimal places for the no-symbol commodity.simonmichael commentedon Feb 4, 2021
However.. numbers in the CSV don't necessarily have a commodity symbol attached. Sometimes it's in another field, sometimes we add it in the rules. So in general it's a little unclear which CSV amounts should be used to infer the number of decimal places for which commodities. Any thoughts ? [And consider that CSV can contain many number fields, with different numbers of decimal places.]
simonmichael commentedon Feb 4, 2021
That said.. in the usual case where you're using the CSV amount as a posting amount, all the decimals are copied to the journal entry and then the display style is inferred as you'd expect.
Here the trouble arises because 1. the posting amount is hard coded to
0with no decimals and 2. there are no other records being processed, which if some of them used the CSV amount as posting amount would cause the display style to use more decimal places. Eg:So I think this part of it is specific to this minimal example, and not much of a problem in practice.
print: show all decimal places in assertion/assignment amounts (#1465)
simonmichael commentedon Feb 4, 2021
So I think that fix I just pushed is enough to resolve this issue, let me know.
;csv: doc: note how CSV amounts affect display style (#1465)
print: don't add decimal places in assertion/assignment amounts either (
;journal: doc: wording tweak for #1465
simonmichael commentedon Feb 4, 2021
Update/summary:
;journal: doc: wording tweak for #1465