18 Format functions for castillian (es-es) 54 FRACTION_STRING_ES = {
78 """ Spanish helper for nice_number 80 This function formats a float to human understandable functions. Like 81 4.5 becomes "4 y medio" for speech and "4 1/2" for text 84 number (int or float): the float to format 85 speech (bool): format for speech (True) or display (False) 86 denominators (iter of ints): denominators to use, default [1 .. 20] 88 (str): The formatted string. 99 whole = round(number, 3)
101 whole, num, den = result
105 strNumber =
'{:,}'.format(whole)
106 strNumber = strNumber.replace(
",",
" ")
107 strNumber = strNumber.replace(
".",
",")
110 return '{} {}/{}'.format(whole, num, den)
114 strNumber = str(whole)
115 strNumber = strNumber.replace(
".",
",")
117 den_str = FRACTION_STRING_ES[den]
123 strNumber =
'un {}'.format(den_str)
126 strNumber =
'{} {}'.format(num, den_str)
131 strNumber =
'{} y {}'.format(whole, den_str)
134 strNumber =
'{} y 1 {}'.format(whole, den_str)
137 strNumber =
'{} y {} {}'.format(whole, num, den_str)
138 if num > 1
and den != 3:
148 Convert a number to it's spoken equivalent 150 For example, '5.2' would return 'cinco coma dos' 153 num(float or int): the number to pronounce (under 100) 154 places(int): maximum decimal places to speak 156 (str): The pronounced number 169 tens = int(num-int(num) % 10)
170 ones = int(num - tens)
171 result += NUM_STRING_ES[tens]
184 result +=
"i" + NUM_STRING_ES[ones]
186 tens = int(num-int(num) % 10)
187 ones = int(num - tens)
188 result += NUM_STRING_ES[tens]
190 result +=
" y " + NUM_STRING_ES[ones]
192 result += NUM_STRING_ES[int(num)]
197 if not num == int(num)
and places > 0:
200 while int(num*place) % 10 > 0
and places > 0:
201 result +=
" " + NUM_STRING_ES[int(num*place) % 10]
209 Format a time to a comfortable human format 211 For example, generate 'cinco treinta' for speech or '5:30' for 215 dt (datetime): date to format (assumes already in local timezone) 216 speech (bool): format for speech (default/True) or display (False)=Fal 217 use_24hour (bool): output in 24-hour/military or 12-hour format 218 use_ampm (bool): include the am/pm for 12-hour format 220 (str): The formatted time string 224 string = dt.strftime(
"%H:%M")
228 string = dt.strftime(
"%I:%M %p")
231 string = dt.strftime(
"%I:%M")
261 elif dt.minute == 40:
264 elif dt.minute == 45:
267 elif dt.minute == 50:
270 elif dt.minute == 55:
277 if hour == 0
or hour == 12:
279 elif hour == 1
or hour == 13:
293 speak +=
" menos cuarto" 301 if minute == 0
and not use_ampm:
312 if hour >= 0
and hour < 6:
313 speak +=
" de la madrugada" 314 elif hour >= 6
and hour < 13:
315 speak +=
" de la mañana" 316 elif hour >= 13
and hour < 21:
317 speak +=
" de la tarde" 319 speak +=
" de la noche"