Format string - spaces between every three digit

python

Solution

This is a bit hacky, but:

format(12345678.46, ',').replace(',', ' ').replace('.', ',')

As described in Format specification mini-language, in a format_spec:

The ',' option signals the use of a comma for a thousands separator.

Then we just replace each comma with a space, then the decimal point with a comma, and we're done.

For more complex cases using `str.format` instead of `format`, the format_spec goes after the colon, as in:

'{:,}'.format(12345678.46)

See PEP 378 for details.

Meanwhile, if you're just trying to use the standard grouping and separators for your system's locale, there are easier ways to do that—the `n` format type, or the `locale.format` function, etc. For example:

>>> locale.setlocale(locale.LC_NUMERIC, 'pl_PL')
>>> format(12345678, 'n')
12 345 678
>>> locale.format('%.2f' 12345678.12, grouping=True)
12 345 678,46
>>> locale.setlocale(locale.LC_NUMERIC, 'fr_FR')
>>> locale.format('%.2f' 12345678.12, grouping=True)
12345678,46
>>> locale.setlocale(locale.LC_ALL, 'en_AU')
>>> locale.format('%.2f' 12345678.12, grouping=True)
12,345,678.46

If your system locale is, say, `pl_PL`, just calling `locale.setlocale(locale.LC_NUMERIC)` (or `locale.setlocale(locale.LC_ALL)`) will pick up the Polish settings that you want, but the same person running your program in Australia will pick up the Australian settings that he wants.

Problem

How to simple format string with decimal number for show it with spaces between every three digits? I can make something like this: ``` some_result = '12345678,46' ' '.join(re.findall('...?', test[:test.find(',')]))+test[test.find(','):] ``` and result is: ``` '123 456 78,46' ``` but I want: ``` '12 345 678,46' ```

Original source