Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localise float notation

In Python a float has the following notation: 35.45. However in Belgium the notation is a bit different: 34,78 . For my thesis it's very important that the floats are printed in the right notation. I could convert every float to a string and change the '.' to a ',' but I wondered if there was any other solution.

like image 740
Driedan Avatar asked Jan 09 '23 09:01

Driedan


1 Answers

You can use str function from locale package:

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

You can also convert localised string to float:

>>> locale.atof("23424,2")
23424.2
like image 108
Daniil Ryzhkov Avatar answered Jan 15 '23 18:01

Daniil Ryzhkov