Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Commit

Permalink
Merge 6b8c8c9 into a1a4beb
Browse files Browse the repository at this point in the history
  • Loading branch information
localhuman committed Jan 25, 2018
2 parents a1a4beb + 6b8c8c9 commit f6df6d6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions neocore/Fixed8.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,13 @@ def ToInt(self):
def ToString(self):
return str(self.value / Fixed8.D)

def ToJsonString(self):
# This is for outputting JSON to match the C# implementation
# Fixed8 are represented as decimal, ( ie 9.123 ) if they aren't whole
# numbers, but when they are whole numbers, it is output as 9
# rather than 9.0. So this strips the trailing .0 for
# this specific use case
return str(self.value / Fixed8.D).rstrip(".0")

def __str__(self):
return self.ToString()
10 changes: 10 additions & 0 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ def test_dunder_methods(self):
self.assertEqual(f8.ToString(), "1.0")
self.assertTrue(isinstance(f8.ToString(), str))

def test_to_string(self):

f8 = Fixed8.FromDecimal(9.123)
self.assertEqual(f8.ToString(), '9.12299999')
self.assertEqual(f8.ToJsonString(), '9.12299999')

f8 = Fixed8.FromDecimal(9)
self.assertEqual(f8.ToString(), '9.0')
self.assertEqual(f8.ToJsonString(), '9')


class BigIntegerTestCase(TestCase):
def test_big_integer_add(self):
Expand Down

0 comments on commit f6df6d6

Please sign in to comment.