Format a number, given the following data: is_negative: true if the number is negative, else false intpart: string of digits that must appear before the decimal point fracpart: string of digits that must come after the point exp: exponent, as an integer spec: dictionary re
(is_negative, intpart, fracpart, exp, spec)
| 6363 | return '' |
| 6364 | |
| 6365 | def _format_number(is_negative, intpart, fracpart, exp, spec): |
| 6366 | """Format a number, given the following data: |
| 6367 | |
| 6368 | is_negative: true if the number is negative, else false |
| 6369 | intpart: string of digits that must appear before the decimal point |
| 6370 | fracpart: string of digits that must come after the point |
| 6371 | exp: exponent, as an integer |
| 6372 | spec: dictionary resulting from parsing the format specifier |
| 6373 | |
| 6374 | This function uses the information in spec to: |
| 6375 | insert separators (decimal separator and thousands separators) |
| 6376 | format the sign |
| 6377 | format the exponent |
| 6378 | add trailing '%' for the '%' type |
| 6379 | zero-pad if necessary |
| 6380 | fill and align if necessary |
| 6381 | """ |
| 6382 | |
| 6383 | sign = _format_sign(is_negative, spec) |
| 6384 | |
| 6385 | if fracpart or spec['alt']: |
| 6386 | fracpart = spec['decimal_point'] + fracpart |
| 6387 | |
| 6388 | if exp != 0 or spec['type'] in 'eE': |
| 6389 | echar = {'E': 'E', 'e': 'e', 'G': 'E', 'g': 'e'}[spec['type']] |
| 6390 | fracpart += "{0}{1:+}".format(echar, exp) |
| 6391 | if spec['type'] == '%': |
| 6392 | fracpart += '%' |
| 6393 | |
| 6394 | if spec['zeropad']: |
| 6395 | min_width = spec['minimumwidth'] - len(fracpart) - len(sign) |
| 6396 | else: |
| 6397 | min_width = 0 |
| 6398 | intpart = _insert_thousands_sep(intpart, spec, min_width) |
| 6399 | |
| 6400 | return _format_align(sign, intpart+fracpart, spec) |
| 6401 | |
| 6402 | |
| 6403 | ##### Useful Constants (internal use only) ################################ |
no test coverage detected