-1

Example:

string = "12.4"
x = int(string)

Just curious if there's a way to do this directly, or if you just have to convert to float then to int as 2 steps.

brownleaf
  • 11
  • 5
  • 6
    As far as I know, you can only do `x = int(float(string))` – Daniel Oct 30 '21 at 11:09
  • yeah there is no other way, but because all integers are a float, so it makes sense, or you can check if is actually an integer ("12.4".isdigit()) and then convert it to float or an int – fmansour Oct 30 '21 at 11:13
  • `int(x.split('.')[0])` -- but that isn't robust since it would fail for things like `'1e4'`. – John Coleman Oct 30 '21 at 11:52

1 Answers1

-1
x="12.4"
x=int(float(x))# this float(x) first converts the string x into float and 
#then int() function converts now newly float x int int x
Beginner
  • 1
  • 1