Python Glossary Русский

all Python 3localefunction

locale.format_string()

numbers and money

Formats numbers with a %-template under the current LC_NUMERIC rules: the local decimal separator and optional thousands grouping.

Syntax

locale.format_string(format, val, grouping=False, monetary=False)

The template is the old %-syntax, not str.format's {}: '%.2f', '%d'. grouping=True only has an effect in a locale that groups digits; in 'C', in force until setlocale(), 1234567 stays ungrouped, because the function does not switch the locale on by itself. Pass a tuple for several values, as with the % operator.

Examples

import locale
print(locale.format_string('%.2f', 1234567.891, grouping=True))  # → 1234567.89 — в C группировки нет
print(locale.format_string('%s: %d', ('итого', 5)))  # → итого: 5
print(f'{1234567:,}')  # → 1,234,567 — группировка без локали

See also

Open in the full glossarydocs.python.orgReport an inaccuracy