[Kmymoney-devel] [Android Port] Help with computing the account balance

Eric Bonney mailinglists at vanhlebarsoftware.com
Tue Oct 9 21:11:42 UTC 2012


Hi Thomas:

Thanks for taking the time to give me such a detailed explanation! Can you
maybe walk me through a few examples that I can try and use as a reference?
I finally can see that what I should have been doing from the start is take
the fraction that is stored in the balance column and just do the division
on that to come up with my correct "value". That is simple enough and
really I am not sure how I missed it.

What I can't figure out, even after trying to apply your explanation, is
how I am seeing values like below in my balance columns.

balance          balanceFormatted
-3133/25               -125.32
726071/50          14521.42
66919/25              2676.76

Since I know most of the time I have the actual value from the user as a
string, how to I take that value and apply the formatMoney() methods to get
to the fraction side? I am struggling trying to take my -125.32 and
returning the fraction of -3133/25.

If you can walk me through some of these that would be fantastic!

Thanks again for all the help.
-Eric

On Sat, Oct 6, 2012 at 5:42 AM, Thomas Baumgart <thb at net-bembel.de> wrote:

> 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<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.
> -------------------------------------------------------------
>
> _______________________________________________
> KMyMoney-devel mailing list
> KMyMoney-devel at kde.org
> https://mail.kde.org/mailman/listinfo/kmymoney-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kmymoney-devel/attachments/20121009/cb00d423/attachment.html>


More information about the KMyMoney-devel mailing list