[Python-ideas] Format mini-language for lakh and crore

Steven D'Aprano steve at pearwood.info
Sun Jan 28 09:05:39 EST 2018

On Sun, Jan 28, 2018 at 09:51:05PM +1000, Nick Coghlan wrote:

> Checking https://www.python.org/dev/peps/pep-0378/, we did suggest
> using the locale module for cases where the engineering style
> groups-of-three structure wasn't appropriate, with the parallel being
> drawn to the fact that you also need to use locale dependent
> formatting to get a decimal separator other than ".".

Or you could use string replacement:

py> format(123456789.25, "0,.3f").replace(',', '_').replace('.', '·')

which may not be quite as convenient or efficient, and it doesn't 
work where groups-of-three aren't appropriate. But on the other hand 
using locale has a number of disadvantages too:

- quoting PEP 378: "Finance users and non-professional programmers find 
  the locale approach to be frustrating, arcane and non-obvious".


- the available locales and their spellings are OS dependent;

- its not cheap, thread-safe or local to your library/function.

The documentation for locale warns:

"It is generally a bad idea to call setlocale() in some library routine, 
since as a side effect it affects the entire program. Saving and 
restoring it is almost as bad: it is expensive and affects other threads 
that happen to run before the settings have been restored."


More information about the Python-ideas mailing list