Formatting question : printing numbers with thousands separators ?

Fred Pacquier fredp at mygale.org.nospam
Fri Aug 31 11:48:07 EDT 2001


"Alex Martelli" <aleax at aleax.it> said :

> Sorry for "hijacking" your response skip, but I didn't get
> Fred's original question!  So -- the locale module is the
> one that serves this need:
> 
>>>> locale.setlocale(locale.LC_NUMERIC, 'us') 'English_United
>>>> States.1252' locale.format("%d",1024*1024,1) '1,048,576' 
>>>>
> Unfortunately, this implies a global, non-thread-safe
> change, and is NOT oriented to just letting one format
> a number once with a given thousand-separator, so it
> may or may not be what one was looking for.

Yup, I read through the module doc pages and the warnings there are quite 
explicit. I guess for specific use in a complex, threaded environment a 
homemade string-massaging routine like those offered by Skip et al would be 
safer. Fortunately (or not) in my case the context is extremely simple (as 
I can't even get a second thread to run, see separate posting :-), so 
"locale" does the job nicely.

-- 
YAFAP : http://www.multimania.com/fredp/



More information about the Python-list mailing list