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
Answers:
convert it to float first and then to int
x = "0.5"
print(int(float(x)))
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)
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
convert it to float first and then to int
x = "0.5"
print(int(float(x)))
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)