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.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@20128 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Christian Stimming
2011-01-20 20:14:40 +00:00
parent f8d09913d0
commit 0c425e1914

View File

@@ -236,7 +236,7 @@ class GncNumeric(GnuCashCoreClass):
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 @@ class GncNumeric(GnuCashCoreClass):
# 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"