In article <[EMAIL PROTECTED]>, Jeremy Reichman <[EMAIL PROTECTED]> wrote:
> I came across the locale module, which looks as if it will do what I want > and perform number formatting (with groupings separated by commas) for a > report I'm trying to generate. It always seems that there's a module for > everything I want to do. > > I'm seeking output like: > > '1,234.56' > '12,345,678,910,111,213,141,516' > > However, the default Python 2.5.1 in Leopard is not formatting numbers for > me in a script. I've also seen the same results from the shell. For example: > > $ python -c 'import locale; print locale.getdefaultlocale(); print > locale.format("%8.2f", 1234.56, 3)' > ('en_US', 'UTF8') > 1234.56 > $ python -c 'import locale; print locale.getdefaultlocale(); print > locale.format("%d", 12345678910111213141516, 3)' > ('en_US', 'UTF8') > 12345678910111213141516 > > I've tried this with and without a call to 'locale.setlocale(locale.LC_ALL, > "")' as I have seen in some examples. It looks like you just need to set LC_NUMERIC, as in: Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin >>> import locale >>> print locale.localeconv() {'mon_decimal_point': '', 'int_frac_digits': 127, 'p_sep_by_space': 127, 'frac_digits': 127, 'thousands_sep': '', 'n_sign_posn': 127, 'decimal_point': '.', 'int_curr_symbol': '', 'n_cs_precedes': 127, 'p_sign_posn': 127, 'mon_thousands_sep': '', 'negative_sign': '', 'currency_symbol': '', 'n_sep_by_space': 127, 'mon_grouping': [], 'p_cs_precedes': 127, 'positive_sign': '', 'grouping': []} >>> print locale.format("%8.2f", 1234.56, True) 1234.56 >>> locale.setlocale(locale.LC_NUMERIC, 'en_US') 'en_US' >>> print locale.localeconv() {'mon_decimal_point': '', 'int_frac_digits': 127, 'p_sep_by_space': 127, 'frac_digits': 127, 'thousands_sep': ',', 'n_sign_posn': 127, 'decimal_point': '.', 'int_curr_symbol': '', 'n_cs_precedes': 127, 'p_sign_posn': 127, 'mon_thousands_sep': '', 'negative_sign': '', 'currency_symbol': '', 'n_sep_by_space': 127, 'mon_grouping': [], 'p_cs_precedes': 127, 'positive_sign': '', 'grouping': [3, 3, 0]} >>> print locale.format("%8.2f", 1234.56, True) 1,234.56 >>> print locale.format("%8.2f", 1234.56, False) 1234.56 -- Ned Deily, [EMAIL PROTECTED] _______________________________________________ Pythonmac-SIG maillist - Pythonmac-SIG@python.org http://mail.python.org/mailman/listinfo/pythonmac-sig