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?