I tried:
import locale
print(locale.locale_alias)
locale.setlocale(locale.LC_ALL, '')
locale.setlocale(locale.LC_NUMERIC, "french")
print(f"{3.14:.2f}")
but the output is 3.14 whereas I would like 3,14.
How to do this with f"…" string formatting?
Note: I don’t want to use .replace(".", ",")
Note: I’m looking for a Windows solution, and solutions from How to format a float with a comma as decimal separator in an f-string? don’t work (thus it’s not a duplicate on Windows):
locale.setlocale(locale.LC_ALL, 'nl_NL')
# or
locale.setlocale(locale.LC_ALL, 'fr_FR')
locale.Error: unsupported locale setting
>Solution :
I looked up the answers on the duplicate flagged page and found an answer that worked:
Change print(f"{3.14:.2f}") to print(f"{3.14:.3n}") and you will get the result: 3,14
See https://docs.python.org/3/library/string.html#format-specification-mini-language:
'n': Number. This is the same as ‘d’, except that it uses the current locale setting to insert the appropriate number separator characters.