[Tutor] commas on numeric input

Peter Otten __peter__ at web.de
Wed Jun 26 08:23:17 CEST 2013


Jim Mooney wrote:

> I thought I read somewhere that Py3 can be set to accept commas as
> part of numeric input, without needing to filter, but now I can't find
> the reference. Is there such a feature, which would be useful, or did
> I see it as part of some numeric module and misremember it?
 
You can set a locale that recognizes the comma as thousands separator:

>>> locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
'en_US.UTF-8'
>>> locale.atoi("1,234,567")
1234567
>>> locale.atof("1,234.56")
1234.56

But be aware that this convention is not universal:

>>> locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
'de_DE.UTF-8'
>>> locale.atof("1,234.56")
1.23456




More information about the Tutor mailing list