close

[Solved] ValueError: invalid literal for int() with base 10

Hello Guys, How are you all? Hope You all Are Fine. Today When I run my code I got the following error ValueError: invalid literal for int() with base 10 in Python. So Here I am Explain to you all the possible solutions here.

Without Wasting your time, Lets start This Article to Solve This Error.

How This ValueError: invalid literal for int() with base 10 Occurs ?

Today When I run my code I got the following error ValueError: invalid literal for int() with base 10.

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

How To Solve this ValueError: invalid literal for int() with base 10 ?

Question: How To Solve this ValueError: invalid literal for int() with base 10 ?
Answer: To solve ValueError: invalid literal for int() with base 10 Just Use Float inside Int will resolve your error. This Error Must have occurred Cause you are passing double value OR float value as Integer. So You just have to use Float OR Double Instead of Integer. Let’s explore our example.

Solution 1

This Error Must have occurred Cause you are passing double value OR float value as Integer. So You just have to use Float OR Double Instead of Integer. Let’s explore our example.

Use Float inside Int will resolve your error.

>>> int(float('55063.000000'))
55063

Solution 2

Below all cases Are acceptable.

  1. Just pass a string representation of an integer into int
>>> int('5')
5
  1. Just pass a string representation of a float into float
>>> float('5.0')
5.0
  1. Just pass a string representation of an integer into float
>>> float('5')
5.0
  1. Just pass a float into int
>>> int(5.0)
5
  1. Just pass an integer into float
>>> float(5)
5.0

Solution 3

Python will convert the number to a float. Simply calling float first then converting that to an int will work: output = int(float(input))

Solution 4

if you have

floatInString = '5.0'

You can convert it to int with floatInInt = int(float(floatInString))

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment