[Pythonmac-SIG] Locale and number formatting

Jeremy Reichman jaharmi at jaharmi.com
Thu May 22 16:37:16 CEST 2008


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.

I've seen some indication in the past (post from 2004) that Mac OS X didn't
support the C locale APIs fully, but I don't know whether that is still the
case or may be an issue here.

Is there a way I can get locale to format numbers?

Thanks!


-- 
Jeremy




More information about the Pythonmac-SIG mailing list