Python Glossary Русский

all Python 3localefunction

locale.str()

numbers and money

Turns a float into a string with the current locale's decimal separator, but with only 12 significant digits.

Syntax

locale.str(float)

This is not the built-in str(): it formats with '%.12g', so locale.str(0.1 + 0.2) gives '0.3' while str() gives '0.30000000000000004', and digits are lost silently. The name str is in locale.__all__, so from locale import * shadows the built-in str for the whole module. For the locale separator use format_string('%.2f', x) or f'{x:n}'.

Examples

import locale
print(locale.str(0.1 + 0.2), str(0.1 + 0.2))  # → 0.3 0.30000000000000004
print(locale.str(1 / 3))  # → 0.333333333333
print('str' in locale.__all__)  # → True — import * заменит встроенный str

See also

Open in the full glossarydocs.python.orgReport an inaccuracy