[Kmymoney-devel] Looking for the code for creating a balance
Thomas Baumgart
thb at net-bembel.de
Sat Oct 1 06:32:01 UTC 2011
Hi,
on Friday 30 September 2011 23:48:24 Eric A. Bonney wrote:
> I am trying to find the code for where a balance for an account is
> created. Looking at the raw data it is tough to see any kind of pattern
> and I was hoping that looking at the code itself would help me in making
> sure that I am putting the correct value into that column.
>
> Example:
> If the balance for an account is 0.00 it appears that KMM stores
> this in the balance column as 0/1, yet if the balance is $127.56 I see
> this stored as 3189/25. If a balance of an account is say just $24 then
> it seems to be stored as 24/1.
>
> Hoping maybe either the code will give me a better understanding or
> maybe someone on the list can help me understand how to determine the
> value to be properly put into the column based on what I have from the
> user.
Use MyMoneyMoney objects and they will take care of reducing the fractions. If
I take your example above, 127.56 is represented as fraction 12756/100 which
can be reduced to 3189/25. The same happens with integers. The denominator is
1 in this case.
MyMoneyMoney always reduces the fraction. In case your input widget returns a
string in the QString variable inputString you can do the following:
MyMoneyMoney val = MyMoneyMoney(inputString);
QString outputString = val.toString();
and outputString now contains 3189/25 when fed with 127.56.
Check the teststeps in void MyMoneyMoneyTest::testStringConstructor() for
further examples. In order to format things, we use
MyMoneyMoney::formatMoney() which will present the decimal form again.
Regarding the storage, it should not matter. Former KMyMoney versions used to
always use a denominator of 100 when storing values. Those could be read by
the newer versions w/o a problem. This also works the other way around.
The actual reduction of the fraction happens inside libalkimia code with the
help of functions found in libgmp if that is what you were looking for. All
this is encapsulated in the MyMoneyMoney object.
http://websvn.kde.org/trunk/extragear/office/alkimia/libalkimia/alkvalue.cpp?revision=1211274&view=markup
look for the AlkValue ctor on line 124. Before each return you find a call to
d->m_val.canonicalize();
which in fact is a call into libgmp's canonicalize function:
http://gmplib.org/manual/Rational-Number-Functions.html#index-
mpq_005fcanonicalize-479
Hope that gives you enough 'food' for the weekend. In case you need more,
please let us know.
--
Regards
Thomas Baumgart
GPG-FP: E55E D592 F45F 116B 8429 4F99 9C59 DB40 B75D D3BA
-------------------------------------------------------------
We had joy, we had fun, we had Linux on our Sun -- anonymous source
-------------------------------------------------------------
-------------- 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/20111001/d3d5c477/attachment.sig>
More information about the KMyMoney-devel
mailing list