diff --git a/bindings/python/gnucash_core.py b/bindings/python/gnucash_core.py index 76d838e0046..6fc9a62cfad 100644 --- a/bindings/python/gnucash_core.py +++ b/bindings/python/gnucash_core.py @@ -320,6 +320,10 @@ def __args_to_instance(args): 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) diff --git a/src/optional/python-bindings/tests/test_numeric.py b/src/optional/python-bindings/tests/test_numeric.py index 3d8f9da8a77..abb0278a7e9 100644 --- a/src/optional/python-bindings/tests/test_numeric.py +++ b/src/optional/python-bindings/tests/test_numeric.py @@ -79,6 +79,11 @@ def test_to_double(self): 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)