r20128 - gnucash/trunk/src/optional/python-bindings - Bug #639906: Adding __str__ and __unicode__ methods to GncNumeric
Christian Stimming
cstim at code.gnucash.org
Thu Jan 20 15:14:40 EST 2011
Author: cstim
Date: 2011-01-20 15:14:40 -0500 (Thu, 20 Jan 2011)
New Revision: 20128
Trac: http://svn.gnucash.org/trac/changeset/20128
Modified:
gnucash/trunk/src/optional/python-bindings/gnucash_core.py
Log:
Bug #639906: Adding __str__ and __unicode__ methods to GncNumeric
Patch by Christoph Holtermann:
1) I decided to let the methods return "Division by Zero" for denom()==0.
2) As proposed in
http://stackoverflow.com/questions/1307014/python-str-versus-unicode i created
both __str__ and __unicode__.
3) I use format to fix the decimal places to 2. It would be nice if the number
would be configurable.
Modified: gnucash/trunk/src/optional/python-bindings/gnucash_core.py
===================================================================
--- gnucash/trunk/src/optional/python-bindings/gnucash_core.py 2011-01-20 20:14:28 UTC (rev 20127)
+++ gnucash/trunk/src/optional/python-bindings/gnucash_core.py 2011-01-20 20:14:40 UTC (rev 20128)
@@ -236,7 +236,7 @@
and GNC_HOW_DENOM_FIXED are available for arithmetic
functions like GncNumeric.add
- Look at gnc-numeric.h to see how ot use these
+ Look at gnc-numeric.h to see how to use these
"""
def __init__(self, num=0, denom=1, **kargs):
@@ -251,6 +251,19 @@
# self.set_denom(denom) # currently undefined
# self.set_num(num) # currently undefined
+ def __unicode__(self):
+ """Returns a human readable numeric value string as UTF8."""
+ if self.denom() == 0:
+ return "Division by zero"
+ else:
+ value_float = self.to_double()
+ value_str = u"{0:.{1}f}".format(value_float,2) ## The second argument is the precision. It would be nice to be able to make it configurable.
+ return value_str
+
+ def __str__(self):
+ """returns a human readable numeric value string as bytes."""
+ return unicode(self).encode('utf-8')
+
class GncPrice(GnuCashCoreClass):
'''
Each priceEach price in the database represents an "instantaneous"
More information about the gnucash-changes
mailing list