Skip to content
Prev Previous commit
Next Next commit
strip "_" from the decimal str, add few tests
  • Loading branch information
skirpichev committed May 30, 2021
commit 6336aef9287d3c07adce3cf32f052006cfb60f7a
6 changes: 3 additions & 3 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ def __new__(cls, numerator=0, denominator=None, *, _normalize=True):
denominator = 1
decimal = m.group('decimal')
if decimal:
decimal = int(decimal)
scale = 10**len(str(decimal))
numerator = numerator * scale + decimal
decimal = decimal.replace('_', '')
scale = 10**len(decimal)
numerator = numerator * scale + int(decimal)
denominator *= scale
exp = m.group('exp')
if exp:
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ def testFromString(self):
self.assertEqual((41, 107), _components(F("1_2_3/3_2_1")))
self.assertEqual((6283, 2000), _components(F("3.14_15")))
self.assertEqual((6283, 2*10**13), _components(F("3.14_15e-1_0")))
self.assertEqual((101, 100), _components(F("1.01")))
self.assertEqual((101, 100), _components(F("1.0_1")))

self.assertRaisesMessage(
ZeroDivisionError, "Fraction(3, 0)",
Expand Down