Re: question formatage de printf - python

Startseite

Nachricht beantworten
Autor: Frédéric
Datum:  
To: guilde
Alte Treads: Re: question formatage de printf
Betreff: Re: question formatage de printf - python
Le lundi 28 mars 2011, Patrice Karatchentzeff a écrit :

> Sinon, on doit pouvoir s'amuser avec les locales mais j'ai un peu la
> flemme de chercher...


En python, ça donne ceci :

$ ipython
Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14)
Type "copyright", "credits" or "license" for more information.

IPython 0.8.4 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.


In [1]: import locale

In [2]: locale.localeconv()
Out[2]:
{'currency_symbol': '',
'decimal_point': '.',
'frac_digits': 127,
'grouping': [],
'int_curr_symbol': '',
'int_frac_digits': 127,
'mon_decimal_point': '',
'mon_grouping': [],
'mon_thousands_sep': '',
'n_cs_precedes': 127,
'n_sep_by_space': 127,
'n_sign_posn': 127,
'negative_sign': '',
'p_cs_precedes': 127,
'p_sep_by_space': 127,
'p_sign_posn': 127,
'positive_sign': '',
'thousands_sep': ''}

In [3]: locale.format("%d", 1325343465, grouping=True)
Out[3]: '1325343465'

In [4]: locale.format("%f", 1325343465.463423, grouping=True)
Out[4]: '1325343465.463423'

In [5]: locale.setlocale(locale.LC_NUMERIC, 'fr_FR')
Out[5]: 'fr_FR'

In [6]: locale.localeconv()
Out[6]:
{'currency_symbol': '',
'decimal_point': ',',
'frac_digits': 127,
'grouping': [3, 3, 0],
'int_curr_symbol': '',
'int_frac_digits': 127,
'mon_decimal_point': '',
'mon_grouping': [],
'mon_thousands_sep': '',
'n_cs_precedes': 127,
'n_sep_by_space': 127,
'n_sign_posn': 127,
'negative_sign': '',
'p_cs_precedes': 127,
'p_sep_by_space': 127,
'p_sign_posn': 127,
'positive_sign': '',
'thousands_sep': ' '}

In [7]: locale.format("%d", 1325343465, grouping=True)
Out[7]: '1 325 343 465'

In [8]: locale.format("%f", 1325343465.463423, grouping=True)
Out[8]: '1325343465,463423'

Curieusement, en float, il n'y a pas le séparateur...

Un lien utile :

http://stackoverflow.com/questions/4082645/using-python-2-xs-locale-module-to-format-numbers-and-currency

--
Frédéric