I am using python v3. I have this string 1,027.86
. I want to convert it to float 1027.86
.
I found a solution while googling.
import locale
locale.setlocale(locale.LC_NUMERIC, "nl")
price = '1,027.86'
price = locale.atof(price)*1000
I searched the documentation on what locale.setlocale(locale.LC_NUMERIC, "nl")
means but could not find an answer.
http://dc.dyu.edu.tw/python/350/library/locale.html
Is there a better argument to put inside setlocal()
that will return the result directly without the need of multiplying by 1000 later?
Specifying
nl
forsetlocale()
is telling it to default to the format for the Netherlands. If you use something likeuk
it should convert correctly as the numeric format is of the formxxx,xxx,xxx.xxx
.This would display: