long scale
parent
f83b4f97bb
commit
5c424fecc1
|
@ -89,7 +89,7 @@ def nice_time(dt, lang="en-us", speech=True, use_24hour=False,
|
|||
return str(dt)
|
||||
|
||||
|
||||
def pronounce_number(number, lang="en-us", places=2):
|
||||
def pronounce_number(number, lang="en-us", places=2, short_scale=True):
|
||||
"""
|
||||
Convert a number to it's spoken equivalent
|
||||
|
||||
|
@ -97,12 +97,15 @@ def pronounce_number(number, lang="en-us", places=2):
|
|||
|
||||
Args:
|
||||
number: the number to pronounce
|
||||
short_scale (bool) : use short (True) or long scale (False)
|
||||
https://en.wikipedia.org/wiki/Names_of_large_numbers
|
||||
Returns:
|
||||
(str): The pronounced number
|
||||
"""
|
||||
lang_lower = str(lang).lower()
|
||||
if lang_lower.startswith("en"):
|
||||
return pronounce_number_en(number, places=places)
|
||||
return pronounce_number_en(number, places=places,
|
||||
short_scale=short_scale)
|
||||
elif lang_lower.startswith("it"):
|
||||
return pronounce_number_it(number, places=places)
|
||||
elif lang_lower.startswith("fr"):
|
||||
|
|
|
@ -223,7 +223,7 @@ def nice_number_en(number, speech, denominators):
|
|||
return return_string
|
||||
|
||||
|
||||
def pronounce_number_en(num, places=2, short_scale=False):
|
||||
def pronounce_number_en(num, places=2, short_scale=True):
|
||||
"""
|
||||
Convert a number to it's spoken equivalent
|
||||
|
||||
|
@ -232,6 +232,8 @@ def pronounce_number_en(num, places=2, short_scale=False):
|
|||
Args:
|
||||
num(float or int): the number to pronounce (under 100)
|
||||
places(int): maximum decimal places to speak
|
||||
short_scale (bool) : use short (True) or long scale (False)
|
||||
https://en.wikipedia.org/wiki/Names_of_large_numbers
|
||||
Returns:
|
||||
(str): The pronounced number
|
||||
"""
|
||||
|
@ -287,11 +289,37 @@ def pronounce_number_en(num, places=2, short_scale=False):
|
|||
res.append(r)
|
||||
return res
|
||||
|
||||
def _split_by_millions(n):
|
||||
assert (0 <= n)
|
||||
res = []
|
||||
while n:
|
||||
n, r = divmod(n, 1000)
|
||||
res.append(r)
|
||||
return res
|
||||
|
||||
def _long_scale(n):
|
||||
if n >= 10e153:
|
||||
return "infinity"
|
||||
n = int(n)
|
||||
assert (0 <= n)
|
||||
res = []
|
||||
for i, z in enumerate(_split_by_millions(n)):
|
||||
if not z:
|
||||
continue
|
||||
number = pronounce_number_en(z, places, True)
|
||||
if i % 2 != 0 and i > 1:
|
||||
number += " " + "thousand"
|
||||
elif i > 0 and i < 3:
|
||||
number += " " + hundreds[i] + ","
|
||||
elif i:
|
||||
number += " " + hundreds[i - 1] + ","
|
||||
res.append(number)
|
||||
return " ".join(reversed(res))
|
||||
|
||||
if short_scale:
|
||||
result += _short_scale(num)
|
||||
else:
|
||||
# TODO long scale
|
||||
result += _short_scale(num)
|
||||
result += _long_scale(num)
|
||||
|
||||
# Deal with fractional part
|
||||
if not num == int(num) and places > 0:
|
||||
|
|
|
@ -140,8 +140,8 @@ class TestPronounceNumber(unittest.TestCase):
|
|||
def test_convert_hundreds(self):
|
||||
self.assertEqual(pronounce_number(100), "one hundred")
|
||||
self.assertEqual(pronounce_number(666), "six hundred and sixty six")
|
||||
# self.assertEqual(pronounce_number(1456), "one thousand, four hundred "
|
||||
# "and fifty six")
|
||||
self.assertEqual(pronounce_number(1456), "one thousand, four hundred "
|
||||
"and fifty six")
|
||||
self.assertEqual(pronounce_number(103254654), "one hundred and three "
|
||||
"million, two hundred "
|
||||
"and fifty four "
|
||||
|
@ -159,6 +159,12 @@ class TestPronounceNumber(unittest.TestCase):
|
|||
"trillion, eight hundred and ninety six billion, six "
|
||||
"hundred and thirty nine million, six hundred and "
|
||||
"thirty one thousand, eight hundred and ninety three")
|
||||
self.assertEqual(pronounce_number(95505896639631893,
|
||||
short_scale=False),
|
||||
"ninety five thousand five hundred and five billion, "
|
||||
"eight hundred and ninety six thousand six hundred "
|
||||
"and thirty nine million, six hundred and thirty one "
|
||||
"thousand, eight hundred and ninety three")
|
||||
|
||||
|
||||
# def nice_time(dt, lang="en-us", speech=True, use_24hour=False,
|
||||
|
|
Loading…
Reference in New Issue