0

I have a function

def float_():
    price_data = {}
    str_num = '3.505,32'
    price_data['price'] = float(str_num.replace('.','').replace(',','.'))
    price_data['currency'] = 'USD'
    return price_data

is there any other way of achieving this float result ?

nexla
  • 434
  • 7
  • 20

1 Answers1

0

You can use locale.atof, as indicated in this question, which abstracts things such as the decimal punctuation and thousands separator:

import locale
locale.setlocale(locale.LC_ALL, 'pt_BR') # Set to the proper language/country

def float_():
    price_data = {}
    str_num = '3.505,32'
    price_data['price'] = locale.atof(str_num)
    price_data['currency'] = 'USD'
    return price_data
Matheus Portela
  • 2,420
  • 1
  • 21
  • 32