John,

Problem is that I have to implement precision directive as discussed with
examples in this post
 https://groups.google.com/forum/#!topic/ledger-cli/m-TgILbfrwA.
In short, if you mention precision 2 means it should round the amounts to 2
places after decimal point. The situation is if I round the amounts
individually, I get rounding error in the total. Therefore, if you multiply
total quantity of a commodity by its final exchange price, it does not
match the calculated balance which should logically happen.
Now in a journal there will be multiple commodities which may have exchange
price. The amount to be rounded must be round of  (total quantity of a
commodity * exchange price ) and then added to the balance.

In order to get this I believe I can sort the list of amounts by commodity.
so that while iterating through the list it calculates value of each amount
and I round at each point where all amounts of a particular commodity have
been valued. The balance will then be round(total of commodity A) + round
(total of commodity B) ... and so on.

Right now, the ledger keeps the amounts in the order of the posts are
parsed.

Consider the following example,  the posts are not sorted by date but by
commodity .

D 1000.000 GBP

2010-01-10 * Buy XXX
    Assets:P                      1 XXX @@ 8.123 GBP
    Assets:C

2010-01-11 * Buy XXX
    Assets:P                      1 XXX @@ 8.123 GBP
    Assets:C

2010-02-10 * Buy XXX
    Assets:P                      1 XXX @@ 8.123 GBP
    Assets:C

2010-03-10 * Buy XXX
    Assets:P                      1 XXX @@ 8.123 GBP
    Assets:C

2010-01-10 * Buy AAA
    Assets:P                      1 AAA @@ 9.123 GBP
    Assets:C

2010-02-10 * Buy AAA
    Assets:P                      1 AAA @@ 9.123 GBP
    Assets:C

2010-03-10 * Buy AAA
    Assets:P                      1 AAA @@ 9.123 GBP
    Assets:C

2010-01-12 * Buy AAA
    Assets:P                      1 AAA @@ 9.123 GBP
    Assets:C


P 2011/01/01 XXX  10.123 GBP
P 2011/01/01 AAA 10.123 GBP

 After valuation  AAA ( 4 * 10,123=40.492 GBP)  should be rounded to 40.49
GBP and XXX ( 4 *  10.123 =40.492 GBP) should be rounded to 40.49  GBP
so the total  balance in account "P" should be 80.980 GBP after rounding.
As of now ,  If I enter posts in any manner it does not matter for ledger
and the balance will always be the same.  But keeping it sorted by
commodity will be helpful for rounding the subtotal of each commodity.
Again, if I would have rounded each amount after valuation I would get
80.960 GBP ( 10.12 + 10.12 ... 8 times) which is not correct.

In the program, I think that I can stable sort the list of posts
(std::list<post_t *>) posts with account object and the amounts map
(std::map<commodity_t *, amount_t>) of balance object to get the desired
order.

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"Ledger" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to