21 _FRACTION_STRING_EN, _LONG_SCALE_EN, _SHORT_SCALE_EN
25 """ English helper for nice_number 27 This function formats a float to human understandable functions. Like 28 4.5 becomes "4 and a half" for speech and "4 1/2" for text 31 number (int or float): the float to format 32 speech (bool): format for speech (True) or display (False) 33 denominators (iter of ints): denominators to use, default [1 .. 20] 35 (str): The formatted string. 41 return str(round(number, 3))
43 whole, num, den = result
50 return '{} {}/{}'.format(whole, num, den)
54 den_str = _FRACTION_STRING_EN[den]
57 return_string =
'a {}'.format(den_str)
59 return_string =
'{} {}'.format(num, den_str)
61 return_string =
'{} and a {}'.format(whole, den_str)
63 return_string =
'{} and {} {}'.format(whole, num, den_str)
71 Convert a number to its spoken equivalent 73 For example, '5.2' would return 'five point two' 76 num(float or int): the number to pronounce (under 100) 77 places(int): maximum decimal places to speak 78 short_scale (bool) : use short (True) or long scale (False) 79 https://en.wikipedia.org/wiki/Names_of_large_numbers 80 scientific (bool): pronounce in scientific notation 82 (str): The pronounced number 86 n, power = number.replace(
"+",
"").split(
"E")
91 return '{}{} times ten to the power of {}{}'.format(
92 'negative ' if float(n) < 0
else '',
94 'negative ' if power < 0
else '',
97 number_names = _NUM_STRING_EN.copy()
98 number_names.update(_SHORT_SCALE_EN)
100 number_names = _NUM_STRING_EN.copy()
101 number_names.update(_LONG_SCALE_EN)
103 digits = [number_names[n]
for n
in range(0, 20)]
105 tens = [number_names[n]
for n
in range(10, 100, 10)]
108 hundreds = [_SHORT_SCALE_EN[n]
for n
in _SHORT_SCALE_EN.keys()]
110 hundreds = [_LONG_SCALE_EN[n]
for n
in _LONG_SCALE_EN.keys()]
115 result =
"negative " if scientific
else "minus " 122 if len(str(num)) == 4
and isinstance(num, int):
127 if _num[1:4] ==
'000' or _num[1:3] ==
'00' or int(_num[0:2]) >= 20:
131 elif _num[2:4] ==
'00':
132 first = number_names[int(_num[0:2])]
133 last = number_names[100]
134 return first +
" " + last
139 first = number_names[int(_num[0:2])]
141 last = number_names[int(_num[2:4])]
143 second = number_names[int(_num[2:3])*10]
144 last = second +
" " + number_names[int(_num[3:4])]
145 return first +
" " + last
148 except Exception
as e:
149 LOG.error(
'Exception in pronounce_number_en: {}' + repr(e))
152 if num
in number_names:
155 result += number_names[num]
157 def _sub_thousand(n):
163 return tens[q - 1] + (
" " + _sub_thousand(r)
if r
else "")
165 q, r = divmod(n, 100)
166 return digits[q] +
" hundred" + (
167 " and " + _sub_thousand(r)
if r
else "")
170 if n >= max(_SHORT_SCALE_EN.keys()):
175 for i, z
in enumerate(_split_by(n, 1000)):
178 number = _sub_thousand(z)
181 number += hundreds[i]
184 return ", ".join(reversed(res))
186 def _split_by(n, split=1000):
190 n, r = divmod(n, split)
195 if n >= max(_LONG_SCALE_EN.keys()):
200 for i, z
in enumerate(_split_by(n, 1000000)):
208 number = number.replace(
',',
'')
209 number +=
" " + hundreds[i+1]
211 return ", ".join(reversed(res))
214 result += _short_scale(num)
216 result += _long_scale(num)
219 if not num == int(num)
and places > 0:
222 while int(num * place) % 10 > 0
and places > 0:
223 result +=
" " + number_names[int(num * place) % 10]
231 Format a time to a comfortable human format 233 For example, generate 'five thirty' for speech or '5:30' for 237 dt (datetime): date to format (assumes already in local timezone) 238 speech (bool): format for speech (default/True) or display (False)=Fal 239 use_24hour (bool): output in 24-hour/military or 12-hour format 240 use_ampm (bool): include the am/pm for 12-hour format 242 (str): The formatted time string 246 string = dt.strftime(
"%H:%M")
250 string = dt.strftime(
"%I:%M %p")
253 string = dt.strftime(
"%I:%M")
272 if string[3:5] ==
'00':
282 if dt.hour == 0
and dt.minute == 0:
284 if dt.hour == 12
and dt.minute == 0:
297 return speak +
" o'clock"