1

I have a list with prices of cars in string ("45.000,00"), I would like to convert this value to float in python.

It looks like this: 45,000.00 in string I want it like this: 45.000.00 in float

  • This is not a duplicate, the string format is different then the already answered question. Here's how you can do it: You need to separate the decimal part and remove the `.` and then convert to float. An example of this could be: value, decimal = '45.000,00'.split(',') as_float = float(value.replace('.','')) + float('0.'+ decimal) – Ícaro Dec 23 '16 at 14:14
  • 1
    @ihhcarus read the second answer, which shows how to do it properly using locales: http://stackoverflow.com/a/6633912/3001761. Also the OP shows three different formats (one of which makes little sense) for what is supposedly the same number, so it isn't entirely clear what the inputs and outputs should be. See also e.g. http://stackoverflow.com/q/13362121/3001761; string mangling probably isn't the best way to approach this. – jonrsharpe Dec 23 '16 at 14:16
  • @jonrsharpe I see, I want straight to the correct answer and skipped that. That could certainly help. – Ícaro Dec 23 '16 at 14:20
  • @ihhcarus `accepted != correct`! – jonrsharpe Dec 23 '16 at 14:20

1 Answers1

1

Just remove the ',' characters before running through float.

float("45,000.00".replace(",", ""))

Edit - misread. You want to remove all '.'s, then replace ','s with '.'s.

float("45.000,00".replace(".", "").replace(",", "."))

However this is a bit hard-coded and nasty - a better solution would be to use some locale library for value handling.

JPEG_
  • 321
  • 1
  • 3
  • 11
  • This is incorrect. You are using `.` as the decimal separator, OP uses `,` since it's BRL money format. – Ícaro Dec 23 '16 at 14:13