0

i have a value which is float and string (it's in quotation marks) and i have to convert a integer value. for example ,when i write x = "1.2", i want take this output:1

i wrote this code:

x = "1.2"
print(int(x))

but i get this error:

Traceback (most recent call last):
  File xxx, line 2, in <module>
    print(int(x))
ValueError: invalid literal for int() with base 10: '1.2'

how can i fix this ?

okydoky
  • 31
  • 1
  • 7
  • `print(int(float('1.2')))` - basically convert it a float (real number) first and then round it as desired – ewcz Dec 07 '17 at 21:42

1 Answers1

1

If you know the string is going to be a float use float(x) to turn it in to a float first. Then you can use int() around that or if you need to round you can use another function like round() to round the number.

print(int(float(x))
NendoTaka
  • 1,224
  • 8
  • 14