Skip to content

Commit

Permalink
Add GncNumeric to native Python Fraction
Browse files Browse the repository at this point in the history
Add helper method to return the native Python fraction type from GncNumeric.
  • Loading branch information
TheBiggerGuy authored and jralls committed Nov 29, 2017
1 parent 1ef379a commit e011576
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bindings/python/gnucash_core.py
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/optional/python-bindings/tests/test_numeric.py
Expand Up @@ -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)
Expand Down

0 comments on commit e011576

Please sign in to comment.