Jerrel Janssen
Jerrel Janssen

Reputation: 1

How can i fix this problem "could not convert string to float

I need to accept inputs in the form of 123.456,78.

num = float(input("enter your annual salary."))
thousands_separator = "."
fractional_separator = ","
num2 ="{:,.2f}".format(num)
print(num2)

hi

50.000,00

Traceback (most recent call last):

File "main.py", line 1, in

ValueError: could not convert string to float: '50.000,00'

Upvotes: 0

Views: 93

Answers (1)

ss3387
ss3387

Reputation: 299

You cannot convert to float with a comma in the string. It only accepts a digit or a decimal point. So convert to a float after you edited it to the way python wants it.

This should help:

num = input("enter your annual salary.")
num = num.replace(".", "")
num = num.replace(",", ".")
num2 ="{:,.2f}".format(float(num))
print(num2)

Upvotes: 1

Related Questions