-1

In Python, how can I parse a numeric string like "323.235" to its corresponding float value, 323.235, or parse the string "67" to an integer, 67.

I just want to know how to parse a float str to a float, and (separately) an integer str to an int.

Alec
  • 8,529
  • 8
  • 37
  • 63
Lincolin
  • 1
  • 2
  • 5
    Sorry how is 545.2222 to be interpreted as 323.235? – EdChum May 20 '19 at 12:22
  • 1
    Use `int()` and `float()` with the string as the argument. – NPE May 20 '19 at 12:23
  • 2
    Possible duplicate of [How do I parse a string to a float or int in Python?](https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int-in-python) – Pratik Kumar May 20 '19 at 12:31
  • 1
    This is literally a word for word copy of the marked duplicate, including the 545.2222 "accidentally" posted initially – Proyag May 20 '19 at 16:52

3 Answers3

0

Try using the following

>>> a = "323.235"
>>> float(a)
323.23520000000004
>>> int(float(a))
323
sridhar er
  • 124
  • 7
0

str to a float

Just apply this

  number= "323.235"
int(float(number))
MIH
  • 125
  • 1
  • 14
0
num = "323.235"
fl = float(num)

You cannot directly convert a string with decimals to an integer, but you can typecast to float first and then use int(), which truncates the float at the decimal point

integer = int(fl)
Alec
  • 8,529
  • 8
  • 37
  • 63