Convert String with "." to int in python

Question:

I want to convert a string with a point to a int
to us it in time

import time

x = "0.5"
time.sleep(int(x))

i tried it with this simple code but i get this error

ValueError: invalid literal for int() with base 10: '0.5'

is there an solutuion to convert the string to int

Asked By: freezy361

||

Answers:

convert it to float first and then to int

x = "0.5"
print(int(float(x)))
Answered By: YUVI_1303

you can convert this string to float and than use floor or ceil funtions for valid int

x = float(x)
x = math.floor(x) # or math.ceil() by your choice
x = int(x)
Answered By: Lior Tabachnik
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.