Revision 979
Added by Aaron Marcuse-Kubitza almost 13 years ago
util.py | ||
---|---|---|
1 | 1 |
# Useful functions and classes |
2 | 2 |
|
3 |
import locale |
|
4 |
|
|
5 |
locale.setlocale(locale.LC_ALL, '') # needed to initialize locale |
|
6 |
|
|
3 | 7 |
#### Function wrappers for statements |
4 | 8 |
|
5 | 9 |
def noop(*args, **kw_args): pass |
... | ... | |
30 | 34 |
|
31 | 35 |
#### Strings |
32 | 36 |
|
37 |
def format_str(format, val): |
|
38 |
return locale.format_string(format, val, grouping=True) |
|
39 |
|
|
33 | 40 |
def to_percent(val, sig_figs=2): |
34 | 41 |
if val >= 1: sig_figs += 1 |
35 |
return (('%#.'+str(sig_figs)+'g') % (val*100))+'%'
|
|
42 |
return format_str('%#.'+str(sig_figs)+'g', val*100)+'%'
|
|
36 | 43 |
|
37 | 44 |
#### Iterables |
38 | 45 |
|
Also available in: Unified diff
util.py: Added format_str() to use locale-specific formatting settings, including thousands separator. Use it in to_percent().