Localize float notation

In Python float has the following notation: 35.45. However, in Belgium, the designation is a little different: 34,78. For my thesis, it is very important that the floats are printed in the correct notation. I could convert each float to a string and change '.'to ',', but I wondered if there was another solution.

+4
source share
1 answer

You can use strthe localepackage function :

>>> import locale
>>> locale.setlocale(locale.LC_ALL, "nl_BE")
'nl_BE'
>>> locale.str(234.2)
'234,2'

You can also convert a localized string to a float:

>>> locale.atof("23424,2")
23424.2
+9
source

All Articles