How to convert Euro currency string to float number?

前端 未结 4 450
滥情空心
滥情空心 2020-12-22 02:44

I need to convert a string currency string in Continental Europe format into a float number:

Input:

\'6.150.593,22 €\'

Realize tha

相关标签:
4条回答
  • 2020-12-22 03:29

    A simple solution may be as follows:

    >>> val = '6.150.593,22 €'
    >>> res = val[:-2].split(',')
    >>> float('.'.join([res[0].replace('.', ''), res[1]]))
    6150593.22
    
    0 讨论(0)
  • 2020-12-22 03:38
    value = '6.150.593,22 €'
    value = value.split()[0]              #Take out euro symbol
    integer, decimal = value.split(',')   #Separate integer and decimals
    integer = integer.replace('.','')     #Take out dots
    final_value = int(integer) + (int(decimal) * (10**(-len(decimal))))
    
    0 讨论(0)
  • 2020-12-22 03:45

    Use locale.atof https://docs.python.org/3/library/locale.html#locale.atof

    >>> import locale
    >>> locale.setlocale(locale.LC_NUMERIC,"nl")
    'nl'
    >>> locale.atof("6.150.593,22")
    6150593.22
    
    0 讨论(0)
  • 2020-12-22 03:46

    A good way of doing it (1 line) :

    NewValue = float(value[:-2].replace(".", "").replace(",","."))
    
    0 讨论(0)
提交回复
热议问题