python convert number with a comma and decimal to a float

Solutions on MaxInterview for python convert number with a comma and decimal to a float by the best coders in the world

showing results for - "python convert number with a comma and decimal to a float"
Adelina
11 Sep 2017
1from locale import atof, setlocale, LC_NUMERIC
2setlocale(LC_NUMERIC, '') # set to your default locale; for me this is
3# 'English_Canada.1252'. Or you could explicitly specify a locale in which floats
4# are formatted the way that you describe, if that's not how your locale works :)
5atof('123,456') # 123456.0
6# To demonstrate, let's explicitly try a locale in which the comma is a
7# decimal point:
8setlocale(LC_NUMERIC, 'French_Canada.1252')
9atof('123,456') # 123.456