[Kmymoney-devel] [Android Port] Help with computing the account balance
Thomas Baumgart
thb at net-bembel.de
Sat Oct 6 09:42:55 UTC 2012
Eric,
On Friday 05 October 2012 13:48:20 you wrote:
> Hi Thomas:
>
> Actually that was the function that I was looking at and referring to. I
> tried looking at it but I'll be honest, I am struggling trying to figure
> out just what exactly it is doing.
>
> I see it takes in a String value currency, which I am assuming is the
> constant defined somewhere for the currency denomination the user is using,
> then it integer for precision and a boolean to for thousands. So I got all
> that down. :)
>
> I am guessing that you are controlling the precision of
> accounts/transactions past two for things like stock prices and
> transactions right? So since I am not implementing this type I can safely
> "ignore" that, but need to keep it in the back of my mind for future
> reference.
Don't forget those currencies that are denominated in 1/1000 (some currencies
used in the middle east are using that). Also, we know of currencies that
don't have a fraction (the Hungarian Forint comes to mind).
>
> It looks like the meat of the actual function starts with the on line 145:
> #if 1
>
> and runs through line 161:
> mpz_class right = (valueRef() - mpq_class(left)) * denom;
>
> I am lost looking at this stuff, the rest of the function looks like it is
> just string manipulation which is easy enough to replicate.
:) It took me a while to write it as well ;)
>
> What is a mpz_class? What is actually going on with lines 152, 154, 160 and
> 161?
Check http://gmplib.org/manual/C_002b_002b-Interface-
Integers.html#C_002b_002b-Interface-Integers for information about the
mpz_class. It is basically an integer with arbitrary precision. mpq_class is a
rational which can be thought of as two mpz_class members (nominator and
denominator). Information for mpq can be found here:
http://gmplib.org/manual/C_002b_002b-Interface-Rationals.html#C_002b_002b-
Interface-Rationals. AlkValue (and thus MyMoneyMoney) simply wrap the
mpq_class value with a nice interface) and a bit of logic not found in libgmp.
'left' in the logic takes the part left of the decimal symbol, 'right' the
part to the right (fraction).
Looking at
value =
static_cast<MyMoneyMoney>(convertDenominator(d)).valueRef().get_num();
For convertDenominator we find in alkvalue.h:
/**
* Returns the current value converted to the given @a denom (default is
100
* or two digits of precision). The rounding method used is controlled by
* the @a how argument and defaults to @p RoundRound.
*/
AlkValue convertDenominator(const int denom = 100, const RoundingMethod how
= RoundRound) const;
The 'current value' is the value of the current object (not the variable
'value' in the above assignment statement. The logic of this method is a bit
tricky. Check the source in libalkimia and also the testcases for it contained
in alkvaluetest.cpp in method
void AlkValueTest::convertDenominator(void)
starting on line 426.
Maybe, I should drop an example here: Let's say, the 'current value' of the
MyMoneyMoney object is "1/2" (or in the decimal world: 0.5). The nominator is
1 and the denominator is 2. Calling convertDenominator(100) on this object
will return a new AlkValue object with nominator 50 and denominator 100 (which
still yields 0.5). Using 'valueRef().get_num()' returns the nominator as
mpz_class object. Now the variable 'value' in the assignment contains '50' in
our example, while d is 100.
Continuing in the MyMoneyMoney code, the assignment of 'left' is the next part
of interest. value is divided by the converted denominator and 50/100 is 0
(treated as an integers (mpz_class)) which is what we need left of the decimal
symbol. 'right' will be assigned the result of "'current value' minus 'left'
times the denomination we want":
right = 0.5 - 0 * 100 = 50
The remainder - as you already noticed - is just a bit of string handling.
Have a nice weekend. If you still need more information, please just ask.
--
Regards
Thomas Baumgart
GPG-FP: E55E D592 F45F 116B 8429 4F99 9C59 DB40 B75D D3BA
-------------------------------------------------------------
It is better to remain silent and be thought a fool,
than to speak, and remove all doubt.
-------------------------------------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 225 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kmymoney-devel/attachments/20121006/030219da/attachment.sig>
More information about the KMyMoney-devel
mailing list