How Do I Parse a String to a Float or Int?
How Can I Convert a Str to Float? "545.2222" → 545.2222 How Can I Convert a Str to Int? "31" → 31 4 30 Answers >>> a = "545.2222" >>> Float(A)...
- How can I convert a
strtofloat?"545.2222" → 545.2222 - How can I convert a
strtoint?"31" → 31
30 Answers
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
Python method to check if a string is a float:
def is_float(value):
try:
float(value)
return True
except:
return False
A longer and more accurate name for this function could be: is_convertible_to_float(value)