snippetpythonCriticalCanonical
How do I parse a string to a float or int?
Viewed 0 times
howparseintfloatstring
Problem
How can I convert an
Or an
For the reverse, see Convert integer to string in Python and Converting a float to a string without rounding it.
Please instead use How can I read inputs as numbers? to close duplicate questions where OP received a string from user input and immediately wants to convert it, or was hoping for
str to a float?"545.2222" -> 545.2222Or an
str to a int?"31" -> 31For the reverse, see Convert integer to string in Python and Converting a float to a string without rounding it.
Please instead use How can I read inputs as numbers? to close duplicate questions where OP received a string from user input and immediately wants to convert it, or was hoping for
input (in 3.x) to convert the type automatically.Solution
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545Code Snippets
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545Context
Stack Overflow Q#379906, score: 3117
Revisions (0)
No revisions yet.