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

Thomas Baumgart thb at net-bembel.de
Wed Oct 10 05:41:53 UTC 2012


Eric,

basically, Allen explained the math behind the operation, and we all remember 
it from school: reducing a fraction. (More inline).

On Tuesday 09 October 2012 17:11:42 you wrote:

> 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!

That is hidden in the MyMoneyMoney string ctor:

MyMoneyMoney::MyMoneyMoney(const QString& pszAmount)
    : AlkValue(pszAmount, _decimalSeparator)
{
}

which calls the AlkValue ctor. It can be used to convert a formatted string 
into a rational value. See testcases in alkvaluetest.cpp, method 
AlkValueTest::stringCtor()

The actual logic is inside the libgmp code in the string assignment e.g.

      d->m_val = qPrintable(QString);    or
      d->m_val = mpq_class(qPrintable(QString));

where d->m_val is of type "mpq_class" and qPrintable returns a "char *"

      d->m_val.canonicalize()

reduces the rational to its smallest representation by removing common factors 
found in numerator and denominator (http://gmplib.org/manual/Rational-Number-
Functions.html#Rational-Number-Functions).

In case you don't use this library you will have to mimic the logic. Allen 
gave an example on how to do that. Be aware of any rounding problems. They 
usually show up when dealing with multiplication of any sort (multiple 
currencies, share value, etc.) in conjunction with addition. You have been 
warned.

-- 

Regards

Thomas Baumgart

GPG-FP: E55E D592 F45F 116B 8429   4F99 9C59 DB40 B75D D3BA
-------------------------------------------------------------
I used to be a hypochondriac AND a kleptomaniac. So I took something for it.
-------------------------------------------------------------
-------------- 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/20121010/6ab5a85f/attachment.sig>


More information about the KMyMoney-devel mailing list