Fix large numbers in short and long scale, include tests

pull/1841/head
Niels Mündler 2018-10-09 00:08:14 +02:00
parent 0f8c6d118d
commit a01f11f30a
2 changed files with 27 additions and 5 deletions

View File

@ -319,13 +319,22 @@ def pronounce_number_en(num, places=2, short_scale=True, scientific=False):
" and " + _sub_thousand(r) if r else "")
def _short_scale(n):
if n > max(SHORT_SCALE_EN.keys()):
return "infinity"
n = int(n)
assert 0 <= n
# TODO Some more or less obvious error to be fixed here
return ", ".join(reversed(
[_sub_thousand(z) + (
" " + hundreds[i] if i else "") if z else ""
for i, z in enumerate(_split_by_thousands(n))]))
res = []
for i, z in enumerate(_split_by_thousands(n)):
if not z:
continue
number = _sub_thousand(z)
if i:
number += " "
number += hundreds[i]
res.append(number)
return " ".join(reversed(res))
def _split_by_thousands(n):
assert 0 <= n
@ -336,7 +345,7 @@ def pronounce_number_en(num, places=2, short_scale=True, scientific=False):
return res
def _long_scale(n):
if n >= 10e153:
if n > max(LONG_SCALE_EN.keys()):
return "infinity"
n = int(n)
assert 0 <= n

View File

@ -186,6 +186,19 @@ class TestPronounceNumber(unittest.TestCase):
"one point six seven two times ten to the power of "
"negative twenty seven")
def test_large_numbers(self):
self.assertEqual(pronounce_number(299792458, short_scale=True),
"two hundred ninety nine million seven hundred ninety two thousand four hundred fifty eight")
self.assertEqual(pronounce_number(299792458, short_scale=False),
"two hundred ninety nine million seven hundred ninety two thousand four hundred fifty eight")
self.assertEqual(pronounce_number(100034000299792458, short_scale=True),
"one hundred quintillion thirty four quadrillion"
"two hundred ninety nine million seven hundred ninety two thousand four hundred fifty eight")
self.assertEqual(pronounce_number(100034000299792458, short_scale=False),
"one hundred trillion thirty four thousand billion"
"two hundred ninety nine million seven hundred ninety two thousand four hundred fifty eight")
# def nice_time(dt, lang="en-us", speech=True, use_24hour=False,
# use_ampm=False):