[Tutor] Formatting Numbers as Strings 33000.50 -> 33,000.50 ?

Remco Gerlich scarblac@pino.selwerd.nl
Wed, 28 Mar 2001 16:17:25 +0200


On Wed, Mar 28, 2001 at 10:06:23PM +0800, Jethro Cramp wrote:
> Some of these are expected, but still none of them are X,XXX.XX. Is this
> some peculiarity of my system (will try it on my Linux box tomorrow)? Or
> should I be using the locale.format() function differently. The idea of
> being able to change the formatting just by changing the locale is very
> appealing (I have clients all over Europe and formatting output to their
> locale would be appreciated by them).

The correct command seems to be 'locale.format("%f", f, 1).

The '1' means that grouping is used, i.e. commas every three figures. As
least, if I read the docs correctly
(http://www.python.org/doc/current/lib/module-locale.html).

Did the setlocale(LC_ALL,"us") work? What's the output of locale.localeconv()?

> This leads me then to the question of how does one use the locale module to
> format a monetary value? If you have a value in (say) DEM what function
> would use to have it output correctly?

The currency symbol is in locale.localeconv()['currency_symbol'],
apparently. I can't test here and I've never used it, so I'm getting it from
the docs...

-- 
Remco Gerlich