Add German thousands separator on Mac OS X in Python

In Mac OS X 10.4 up to 10.15 the German locales (de_DE*, de_CH* and de_AT*) are missing a thousands separator. This can be shown using Python:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.format_string("%.2f", 1234.34, grouping=True)
'1234,34'

When using Python, the MacOS installation does not need to be altered as described in Add missing thousands separator to German locale on Mac OS X. It is enough to change the locale configuration during runtime like the following:

import locale

locale.setlocale(locale.LC_ALL, 'de_DE')
locale._override_localeconv["thousands_sep"] = "."
locale._override_localeconv["grouping"] = [3, 3, 0]

This leads to:

>>> locale.format_string("%.2f", 1234.34, grouping=True)
'1.234,34'

Tested in Python 3.7 and 3.8.

Idea taken from Stack Overflow.

About Michael Howitz

I am a software developer at gocept in Halle (Saale). To develop software, I mainly use Python, Zope, ZTK and Django.
This entry was posted in Mac OS X, Python and tagged , , , , . Bookmark the permalink.

1 Response to Add German thousands separator on Mac OS X in Python

  1. Pingback: Add missing thousands separator to German locale on Mac OS X | icemac

Leave a comment