This question already has answers here:
Convert a string to integer with decimal in Python
(8 answers)
Closed 4 months ago.
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)
Related
This question already has answers here:
How can I convert a string with dot and comma into a float in Python
(9 answers)
Closed 10 months ago.
the string will be the price of some product, so it will basically come like this '1,433.10', the problem is that I need to compare it with the value that the user enters in an input that is only possible to enter integers because of the isdigit() method , used to check if the input is a number, and this causes the comparison to fail.
I already tried converting to int, to float and nothing worked, it only generated exceptions
def convert_values():
price = results['price'][2:] # here is where the string with the value is, which in this case is '1,643.10'
print(int(float(price))) # if I try to cast just to int: ValueError: invalid literal for int() with base 10: '1,643.10'
Before converting, replace the commas in the string with '' as:
price = results['price'][2:].replace(',', '')
print(int(float(price)))
This question already has answers here:
How to convert a float string to an integer in python 3 [duplicate]
(4 answers)
Closed 12 months ago.
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.
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
This question already has answers here:
Convert decimal mark when reading numbers as input
(8 answers)
Closed 2 years ago.
As the title says, how would I go about converting '1,0' into 1,0 I've been getting ValueError: invalid literal for int() with base 10: '1,0'
First of all, 1,0 is generally not valid syntax for a float. You have to use 1.0. Second you can't convert 1.0 to an int, as this is a float. Use float("1.0") instead. If you need an int you can round the parsed float, e.g.
round(float("1.0"))
you can use:
int(float('1,0'.replace(',', '.')))
This question already has answers here:
In Python, how do I convert all of the items in a list to floats?
(14 answers)
Closed 5 years ago.
t = ["3.4","7","","-1.3","-5",""]
How to convert this list into integer and float for further ananlysis?
I have done this so far but i got error.
t = ["3.4","7","","-1.3","-5",""]
def integer_float(string):
try:
return int(string)
except ValueError:
return float(string)
for i in t:
integer_float(i)
I got the error
ValueError: could not convert string to float:
because you can't parse empty string as int nor float. try:
int(string or "0")
This question already has an answer here:
Turning a list of strings into float
(1 answer)
Closed 9 years ago.
I should get this converted to numbers with float().
How can I do it?
Here is the code I have problems with. It´s simplified to the problem
poly = input().split()
poly.reverse()
return poly
Simply as you said in your question...you can use float:
>>> string = "1234.567"
>>> float(string)
1234.567
You can convert a string to a number in Python with the int(), float() and long() built-in functions.
E.g.
return int(poly)
See the Python docs for details:
int()
float()
long()
If poly is a list of strings you want to convert in floats, do:
floats = [float(s) for s in poly]