0

Why do I get an error When I tried to use int() function to convert a float to integer?

>>> int('99.99')
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    int('99.99')
ValueError: invalid literal for int() with base 10: '99.99'

I expected the result to be 99

4
  • You're not converting a float, you're converting a string.
    – Barmar
    Commented Jun 21, 2019 at 0:58
  • 2
    You aren't converting a float to an int; you are trying to convert a string containing a float literal. In the spirt of "explicit is better than implicit", int won't try to go from string to float to int implicitly. If you want to do this, be explicit: int(float('99.99')).
    – chepner
    Commented Jun 21, 2019 at 0:58
  • This should get you what you want int(float('99.99'))
    – tchau.dev
    Commented Jun 21, 2019 at 0:59
  • You are converting from string to int, it is failing because python doesn't expect an int to have a dot in it. Try round(float('99.99)). Maybe look at Math.floor/ceil depending on how you want it to round your number.
    – solarc
    Commented Jun 21, 2019 at 1:00

3 Answers 3

2

Your argument isn't a float, it's a string containing the representation of a float. You have to convert it to a float first, then you can convert that to an int.

int(float('99.99'))
3
  • Thanks, why can I get 100 using: int('100')
    – qiao
    Commented Jun 21, 2019 at 1:27
  • Because 100 is an integer.
    – Barmar
    Commented Jun 21, 2019 at 1:28
  • int() will convert a string to an integer if it contains the representation of an integer. If there are digits after the decimal point, it's not a valid integer.
    – Barmar
    Commented Jun 21, 2019 at 1:29
0

Per the docs

Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, return x.int(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base.

Pay particular attention to "representing an integer literal". So your str that you are attempting to convert cannot be a float, because that's a float literal, not an int literal.

So, as others have noted, you cannot go directly from a float literal to an int, you need to convert the float first:

x = '123.45'

int(float(x))

0

You are getting a ValueError because you are overloading int() with an argument that is not consistent with the Python docs.

According to the doc:

"If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal "

Basically, x (in your case '99.99') is the string 99.99 which does not satisfy the requirements of being an integer literal. You provided a floating literal.

TL;DR

int(float('99.99'))
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.