Formate a number with commas

2012-02-09 Thread noydb
How do you format a number to print with commas? Some quick searching, i came up with: import locale locale.setlocale(locale.LC_ALL, ) locale.format('%d', 2348721, True) '2,348,721' I'm a perpetual novice, so just looking for better, slicker, more proper, pythonic ways to do this. Thanks!

Re: Formate a number with commas

2012-02-09 Thread Neil Cerutti
On 2012-02-09, noydb jenn.du...@gmail.com wrote: How do you format a number to print with commas? Some quick searching, i came up with: import locale locale.setlocale(locale.LC_ALL, ) locale.format('%d', 2348721, True) '2,348,721' I'm a perpetual novice, so just looking for better,

Re: Formate a number with commas

2012-02-09 Thread Peter Otten
noydb wrote: How do you format a number to print with commas? Some quick searching, i came up with: import locale locale.setlocale(locale.LC_ALL, ) locale.format('%d', 2348721, True) '2,348,721' I'm a perpetual novice, so just looking for better, slicker, more proper, pythonic

Re: Formate a number with commas

2012-02-09 Thread Chris Rebert
On Thu, Feb 9, 2012 at 12:39 PM, Peter Otten __pete...@web.de wrote: noydb wrote: How do you format a number to print with commas? Some quick searching, i came up with: import locale locale.setlocale(locale.LC_ALL, ) locale.format('%d', 2348721, True) '2,348,721' I'm a perpetual

Re: Formate a number with commas

2012-02-09 Thread Alain Ketterlin
noydb jenn.du...@gmail.com writes: How do you format a number to print with commas? import locale locale.setlocale(locale.LC_ALL, ) This sets the locale according to the environment (typically LANG---I'm talking about linux, don't know others). locale.format('%d', 2348721, True)

Re: Formate a number with commas

2012-02-09 Thread Peter Otten
Chris Rebert wrote: On Thu, Feb 9, 2012 at 12:39 PM, Peter Otten __pete...@web.de wrote: import locale locale.setlocale(locale.LC_ALL, ) 'de_DE.UTF-8' {:n}.format(1234) # locale-aware '1.234' {:,d}.format(1234) # always a comma '1,234' The latter requires Python 3.1+ and is courtesy

Re: Formate a number with commas

2012-02-09 Thread Chris Rebert
On Thu, Feb 9, 2012 at 1:16 PM, Peter Otten __pete...@web.de wrote: Chris Rebert wrote: On Thu, Feb 9, 2012 at 12:39 PM, Peter Otten __pete...@web.de wrote: import locale locale.setlocale(locale.LC_ALL, ) 'de_DE.UTF-8' {:n}.format(1234) # locale-aware '1.234' {:,d}.format(1234) # always a

Re: Formate a number with commas

2012-02-09 Thread John Posner
On 2:59 PM, noydb wrote: How do you format a number to print with commas? I would readily admit that both the locale module and format method are preferable to this regular expression, which certainly violates the readability counts dictum: r(?=\d)(?=(\d\d\d)+$) # python 2.6.6 import re

Re: Formate a number with commas

2012-02-09 Thread Terry Reedy
On 2/9/2012 5:12 PM, Chris Rebert wrote: Argh. The 2.7 docs say it was added in 2.7, but the 3.3a0 docs say it was added in 3.1 (Guido's backporting time machine messes with causality). Python 2 docs refer to Python 2. Python 3 docs refer to Python 3. So 'it' was in neither 2.6 nor 3.1.