Add GncNumeric to native Python Fraction

Add helper method to return the native Python fraction type from GncNumeric.
pull/241/head
Guy Taylor 9 years ago committed by John Ralls
parent 1ef379a704
commit e011576e37

@ -320,6 +320,10 @@ class GncNumeric(GnuCashCoreClass):
else:
raise TypeError('Required single int/float/str or two ints: ' + str(args))
def to_fraction(self):
from fractions import Fraction
return Fraction(self.num(), self.denom())
def __unicode__(self):
"""Returns a human readable numeric value string as UTF8."""
return gnc_numeric_to_string(self.instance)

@ -79,6 +79,11 @@ class TestGncNumeric( TestCase ):
for test_num in [0.0, 1.1, -1.1, 1/3.0]:
self.assertEqual(GncNumeric(test_num).to_double(), test_num)
def test_to_fraction(self):
fraction = GncNumeric("1000/3").to_fraction()
self.assertEqual(fraction.numerator, 1000)
self.assertEqual(fraction.denominator, 3)
def test_incorect_args(self):
with self.assertRaises(TypeError):
GncNumeric(1, 2, 3)

Loading…
Cancel
Save