Convert decimal mark when reading numbers as input Convert decimal mark when reading numbers as input python python

Convert decimal mark when reading numbers as input


You may do it the locale-aware way:

import locale# Set to users preferred locale:locale.setlocale(locale.LC_ALL, '')# Or a specific locale:locale.setlocale(locale.LC_NUMERIC, "en_DK.UTF-8")print locale.atof("3,14")

Read this section before using this method.


float("2,5".replace(',', '.')) will do in most cases

If valueis a large number and .has been used for thousands, you can:

Replace all commas for points: value.replace(",", ".")

Remove all but the last point: value.replace(".", "", value.count(".") -1)