Home > Back-end >  ValueError: could not convert string to float: '$2,464.34'
ValueError: could not convert string to float: '$2,464.34'

Time:11-21

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

CodePudding user response:

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')

  • Related