I am searching for the proper way of German number formatting (e.g. 1.000,1234
) in Python under Windows OS.
I tried locale.setlocale
but did not succeed.
Instead, I have written a function to come up with the desired output.
Is there a better way?
def ger_num(number, precision=3):
"""
returns german formatted number as string or an empty string
"""
if number is not None:
try:
my_number = "{:,f}".format(number)
except ValueError:
return ""
decimals, fraction = my_number.split(".")[0], my_number.split(".")[1]
decimals = decimals.replace(",", ".")
if precision:
return decimals + "," + fraction[:precision]
else:
return decimals
else:
return ""
locale
? Perhaps your platform doesn't support it? See the many answers here: #14287551 – Disinfect