ValueError: could not convert string to float: '$2,464.34'


I am trying to convert the data to float in order make it as numerical format in excel to sort the data i am getting error.wherever the float in mentioned i did it now but previously there was no float .

 def get_output_value(self, key, value, neutral=None):
    display = value
    if value is None and not neutral.person.is_active:
        return '-', '-'

    if value is None:
        return float(f"${Decimal('.00')}"), float(f"${Decimal('.00')}")

    if isinstance(value, Decimal):
        return float(f"${intcomma(value.quantize(Decimal('.00')))}"), float(f"${intcomma(display.quantize(Decimal('.00')))}")

    return float(value), display
Asked By: Happy



The answer is in the error message in this case. '$2,464.34' is a string with $ and , characters in it, but float() expects a number-like input.

TLDR, you want float('2464.34') but you’re giving float('$2,464.34')

Answered By: Mike L
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.