Update: The answers show so far that it seems to be a platform-related bug on OSX that has to do with the specific locale settings as they don't fully support grouping numbers.
Update 2: I have just opened an issue on Python's bug tracker. Let's see if there is a solution to this problem.
I want to format integer and float numbers according to the German numbering convention. This is possible using the format language and the presentation type n
but fails on my platform.
- Platform: OS X 10.8.2 (Mountain Lion)
- Python: 2.7.3 64-bit
(v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Examples:
1234
=>1.234
1234.56
=>1.234,56
1000000
=>1.000.000
What I have tried so far:
Setting the German locale
import locale locale.setlocale(locale.LC_ALL, 'de_DE')
The format specification option
,
only recognizes the English format.'{:,}'.format(1234) '1,234' '{:,}'.format(1234.56) '1,234.56' '{:,}'.format(1000000) '1,000,000'
According to the Python docs, the integer and float presentation type
n
is supposed to do what I want but it doesn't.'{:n}'.format(1234) '1234' '{:n}'.format(1234.56) '1234,56' # at least the comma was set correctly here '{:n}'.format(1000000) '1000000' '{:n}'.format(12345769.56) '1,23458e+07' # it's doing weird things for large floats
Some more examples and comparisons inspired by @J.F.Sebastian:
for n in [1234, 1234.56, 1000000, 12345769.56]: print('{0:,} {0:n}'.format(n)) fmt, val = "%d %f", (n, n) print(fmt % val) print(locale.format_string(fmt, val)) print(locale.format_string(fmt, val, grouping=True)) print('-'*60)
This yields the following incorrect results on my platform:
1,234 1234 1234 1234.000000 1234 1234,000000 1234 1234,000000 ------------------------------------------------------------ 1,234.56 1234,56 1234 1234.560000 1234 1234,560000 1234 1234,560000 ------------------------------------------------------------ 1,000,000 1000000 1000000 1000000.000000 1000000 1000000,000000 1000000 1000000,000000 ------------------------------------------------------------ 12,345,769.56 1,23458e+07 12345769 12345769.560000 12345769 12345769,560000 12345769 12345769,560000 ------------------------------------------------------------
The correct results which I'm not getting would look like that:
1,234 1.234 1234 1234.000000 1234 1234,000000 1.234 1.234,000000 ------------------------------------------------------------ 1,234.56 1.234,56 1234 1234.560000 1234 1234,560000 1.234 1.234,560000 ------------------------------------------------------------ 1,000,000 1.000.000 1000000 1000000.000000 1000000 1000000,000000 1.000.000 1.000.000,000000 ------------------------------------------------------------ 12,345,769.56 1,23458e+07 12345769 12345769.560000 12345769 12345769,560000 12.345.769 12.345.769,560000 ------------------------------------------------------------
Do you have a solution for me using the format language only? Is there any way to trick the locale settings on my platform to accept grouping?
This worked for me when used with the German locale:
This is in Cygwin under Windows 7:
I was asked by @Lattyware to provide my own solution for including separators according to the German numbering convention without using the format language. Here is the best solution that I can come up with:
The performance is also quite okay, compared to the solution given by @Jon-Eric.
If you know how my solution could be optimized, please let me know.
Super ugly, but technically answers the question:
From PEP 378:
Python's
locale
module's implementation unfortunately varies quite a bit across platforms. It's really just a light wrapper around the C library vendor's notion of locales.So, on Windows 7, with Python 2.7.3 64-bit, this happens to work (note: locales have different names in Windows):
Whether the thousands separator will be used can be determined by examining the "local conventions":
Even more ugly with
split
,join
andreplace
: