pronounce scientific notation

pull/1653/head
jarbasal 2018-06-21 23:05:03 +01:00
parent 2f05b0b820
commit 1710803bb0
3 changed files with 29 additions and 3 deletions

View File

@ -89,7 +89,8 @@ def nice_time(dt, lang="en-us", speech=True, use_24hour=False,
return str(dt)
def pronounce_number(number, lang="en-us", places=2, short_scale=True):
def pronounce_number(number, lang="en-us", places=2, short_scale=True,
scientific=False):
"""
Convert a number to it's spoken equivalent
@ -99,13 +100,15 @@ def pronounce_number(number, lang="en-us", places=2, short_scale=True):
number: the number to pronounce
short_scale (bool) : use short (True) or long scale (False)
https://en.wikipedia.org/wiki/Names_of_large_numbers
scientific (bool) : convert and pronounce in scientific notation
Returns:
(str): The pronounced number
"""
lang_lower = str(lang).lower()
if lang_lower.startswith("en"):
return pronounce_number_en(number, places=places,
short_scale=short_scale)
short_scale=short_scale,
scientific=scientific)
elif lang_lower.startswith("it"):
return pronounce_number_it(number, places=places)
elif lang_lower.startswith("fr"):

View File

@ -221,7 +221,7 @@ def nice_number_en(number, speech, denominators):
return return_string
def pronounce_number_en(num, places=2, short_scale=True):
def pronounce_number_en(num, places=2, short_scale=True, scientific=False):
"""
Convert a number to it's spoken equivalent
@ -232,9 +232,18 @@ def pronounce_number_en(num, places=2, short_scale=True):
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
scientific (bool): pronounce in scientific notation
Returns:
(str): The pronounced number
"""
if scientific:
number = '%E' % num
n, power = number.replace("+", "").split("E")
power = int(power)
if power != 0:
return pronounce_number_en(float(n), places, short_scale, False) + \
" times ten to the power of " + \
pronounce_number_en(power, places, short_scale, False)
if short_scale:
number_names = NUM_STRING_EN.copy()
number_names.update(SHORT_SCALE_EN)

View File

@ -166,6 +166,20 @@ class TestPronounceNumber(unittest.TestCase):
"and thirty nine million, six hundred and thirty one "
"thousand, eight hundred and ninety three")
def test_convert_scientific_notation(self):
self.assertEqual(pronounce_number(0, scientific=True), "zero")
self.assertEqual(pronounce_number(33, scientific=True),
"three point three times ten to the power of one")
self.assertEqual(pronounce_number(299792458, scientific=True),
"two point nine nine times ten to the power of eight")
self.assertEqual(pronounce_number(299792458, places=6,
scientific=True),
"two point nine nine seven nine two five times "
"ten to the power of eight")
self.assertEqual(pronounce_number(1.672e-27, places=3,
scientific=True),
"one point six seven two times ten to the power of "
"negative twenty seven")
# def nice_time(dt, lang="en-us", speech=True, use_24hour=False,
# use_ampm=False):