In python 2, the result of a division of two integers is always a integer. To force a float division, you need to use at least one float in the division:

ratio = float(inputLines)/outputLines

Be careful not to do ratio = float(inputLines/outputLines): although that results in a float, it's one obtained after doing the integer division, so the result will be "wrong" (as in "not what you expect")

In python 3, the integer division behavior was changed, and a division of two integers results in a float. You can also use this functionality in python 2.7, by putting from __future__ import division in the begging of your files.


The reason ratio = 1.0 does not work is that types (in python) are a property of values, not variables - in other words, variables don't have types.

a= 1.0 # "a" is a float
a= 1   # "a" is now a integer
Answer from loopbackbee on Stack Overflow
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert integer to double in python
5 Best Ways to Convert Integer to Double in Python - Be on the Right Side of Change
February 18, 2024 - This method is straightforward and widely accepted for its simplicity and readability. ... This code snippet converts the integer 42 to a double by using the float() function, which returns 42.0.
Discussions

How to double input number in python?
Multiply it by 2. You'll likely want to convert it to an integer first, though. More on reddit.com
🌐 r/learnpython
6
2
September 20, 2022
Converting large int value to float and back to int messes up the value?
There's a long-winded and very technical explanation about how floats and ints are implemented and why this happens for those numbers, but the reason why it must happen for some numbers is pretty simple: Python ints are unbounded, which means there are infinitely many ints you can have in Python. Python floats are 64-bit numbers, which means there are only 2^64 of them. Since there aren't infinitely many floats, you can't make int -> float -> int work for all numbers. The technical explanation is that ints are stored as a list of digits (essentially), so it can be as long as you want, but floats are basically a fraction, stored as a triple of numbers (s,p,q) each of a fixed-size and the numeric value of the float is (-1)^s*q*2^p. If this doesn't make sense, read up on "double-precision floating point" https://en.wikipedia.org/wiki/Double-precision_floating-point_format . The consequence of this is that floats can store very small and very large numbers, but there's a lot of gaps in between, and if your int falls in one of those gaps then when you turn it into a float with float() it picks the "nearest" one instead, so when you turn it back to an int you get the wrong value. More on reddit.com
🌐 r/learnpython
12
9
February 22, 2024
convert int to double
aryn weatherly is having issues with: I've tried working this several different ways and keep making things worse and worse. please help More on teamtreehouse.com
🌐 teamtreehouse.com
1
March 20, 2020
How do I convert an int directly to a double?
From your error message I wonder, how are you using the doubleValue() function? it should be used var.doubleValue() not doubleValue(var), thought it would be good to double check More on reddit.com
🌐 r/processing
8
4
September 15, 2019
People also ask

What happens if you try to convert a non-numeric string to an integer in Python?

A ValueError will be raised indicating that the string is not a valid integer.

int('abc')  # Raises ValueError: invalid literal for int() with base 10: 'abc'
🌐
datacamp.com
datacamp.com › home › tutorials › python › python data type conversion: a guide with examples
Python Data Type Conversion: A Guide With Examples | DataCamp
How do you convert a list of integers to a single string in Python?

You can use the join() method combined with map() to convert each integer to a string and concatenate them.

int_list = [1, 2, 3, 4]
result = ''.join(map(str, int_list))
print(result)  # Output: '1234'
🌐
datacamp.com
datacamp.com › home › tutorials › python › python data type conversion: a guide with examples
Python Data Type Conversion: A Guide With Examples | DataCamp
Can you convert a set to a list in Python? How?

Yes, you can convert a set to a list using the list() function.

a_set = {1, 2, 3}
a_list = list(a_set)
print(a_list)  # Output: [1, 2, 3]
🌐
datacamp.com
datacamp.com › home › tutorials › python › python data type conversion: a guide with examples
Python Data Type Conversion: A Guide With Examples | DataCamp
🌐
DataCamp
datacamp.com › home › tutorials › python › python data type conversion: a guide with examples
Python Data Type Conversion: A Guide With Examples | DataCamp
February 16, 2025 - Data conversion in Python can happen in two ways: either you explicitly tell the compiler to convert a data type to some other type, or the compiler understands this by itself and does it for you. In the former case, you're performing an explicit data type conversion, whereas, in the latter, you're doing an implicit data type conversion. Implicit conversion or coercion is when data type conversion takes place either during compilation or during run time and is handled directly by Python for you. Let's see an example: a_int = 1 b_float = 1.0 c_sum = a_int + b_float print(c_sum) print(type(c_sum))
🌐
Manifoldapp
cuny.manifoldapp.org › read › how-to-code-in-python-3 › section › 517db09e-e5a2-4b02-bd3d-d5406a3b40a8
How To Convert Data Types | How To Code in Python 3 | Manifold @CUNY
Sometimes you are working on someone else’s code and will need to convert an integer to a float or vice versa, or you may find that you have been using an integer when what you really need is a float. Python has built-in methods to allow you to easily convert integers to floats and floats to integers.
🌐
Reddit
reddit.com › r/learnpython › how to double input number in python?
How to double input number in python? : r/learnpython
September 20, 2022 - Multiply it by 2. You'll likely want to convert it to an integer first, though. ... Literally just print(numb * 2). You can also create a variable to store it in such as numb2 = num * 2. Then print(numb2) ... name = input("Enter your name: ") print("Hello {}, welcome to my program.".format(name)) ...
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-convert-data-types-in-python-3
How To Convert Data Types in Python 3 | DigitalOcean
August 20, 2021 - This Python 3 tutorial will guide you through converting data types including numbers, strings, tuples and lists, as well as provide examples to help familia…
🌐
W3Schools
w3schools.com › python › python_casting.asp
Python Casting
Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types. Casting in python is therefore done using constructor functions: int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number)
🌐
Kaust-vislab
kaust-vislab.github.io › python-novice-gapminder › 03-types-conversion
Data Types and Type Conversion – Introduction to Python for Data Science
September 28, 2020 - Use built-in functions to convert between integers, floating point numbers, and strings. Every value in a program has a specific type. Integer (int): represents positive or negative whole numbers like 3 or -512. Floating point number (float): represents real numbers like 3.14159 or -2.5. Character string (usually called “string”, str): text. Written in either single quotes or double ...
🌐
Reddit
reddit.com › r/learnpython › converting large int value to float and back to int messes up the value?
r/learnpython on Reddit: Converting large int value to float and back to int messes up the value?
February 22, 2024 -

Say I have a large int value

>>> 7 ** 69
20500514515695490612229010908095867391439626248463723805607

Now let's say I convert it to a float value.

>>> float(7 ** 69)
2.050051451569549e+58

Seems about right. Now if I convert it back to back into an int...

>>> int(float(7 ** 69))
20500514515695489879650927979797501063745685669370929348608

Now it's a very different number. Why did that happen?

I am sorry if it's a stupid question but this has me very intrigued.

Top answer
1 of 11
35
There's a long-winded and very technical explanation about how floats and ints are implemented and why this happens for those numbers, but the reason why it must happen for some numbers is pretty simple: Python ints are unbounded, which means there are infinitely many ints you can have in Python. Python floats are 64-bit numbers, which means there are only 2^64 of them. Since there aren't infinitely many floats, you can't make int -> float -> int work for all numbers. The technical explanation is that ints are stored as a list of digits (essentially), so it can be as long as you want, but floats are basically a fraction, stored as a triple of numbers (s,p,q) each of a fixed-size and the numeric value of the float is (-1)^s*q*2^p. If this doesn't make sense, read up on "double-precision floating point" https://en.wikipedia.org/wiki/Double-precision_floating-point_format . The consequence of this is that floats can store very small and very large numbers, but there's a lot of gaps in between, and if your int falls in one of those gaps then when you turn it into a float with float() it picks the "nearest" one instead, so when you turn it back to an int you get the wrong value.
2 of 11
11
You've stumbled across the limitations of floating point numbers: https://docs.python.org/3/tutorial/floatingpoint.html If you look at the first 15 digits of the int and float, you'll see that the values are the same. This is because behind the scenes the float will only have 16 digits of precision due to how they are stored. Python has a library called decimals that can be used to improve precision. https://docs.python.org/3/library/decimal.html
🌐
Sage Q&A Forum
ask.sagemath.org › question › 8088 › convert-from-int-to-double-and-back-in-cython
Convert from int to double and back in Cython - ASKSAGE: Sage Q&A Forum
April 21, 2011 - If you use the @cython.cdivision(True) decorator, cython will not add exception checking for division by zero, this allows the division statement to be converted to one line of pure C code. In your code j is never zero, so this is okay. Also, you can skip the double t entirely if you just write fact/j. Since both are integers, the division occurs as integers and the result is the floor of the rational number fact/j.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-float-to-int-in-python
How to convert Float to Int in Python? - GeeksforGeeks
May 10, 2025 - n = 9.3 print('Original type:', type(n).__name__) n = int(n) print('Converted value:', n, ', Type:', type(n).__name__) ... Type changes from float to int. The int() function in Python removes the decimal part and keeps only the whole number. However, due to how floating-point numbers are stored in memory, a value like 5.99999999999999999999 might be treated as 6.0.
🌐
Python
docs.python.org › 2.4 › lib › typesnumeric.html
https://docs.python.org/2.4/lib/typesnumeric.html
November 18, 2006 - Integer literals with an "L" or "l" suffix yield long integers ("L" is preferred because "1l" looks too much like eleven!). Numeric literals containing a decimal point or an exponent sign yield floating point numbers. Appending "j" or "J" to a numeric literal yields a complex number with a zero real part.
🌐
Replit
replit.com › home › discover › how to convert an int to a string in python
How to convert an int to a string in Python | Replit
April 14, 2026 - For very large or small numbers, {:.2e} converts the integer to scientific notation with two decimal places, producing 1.23e+09. These tools are especially useful in data visualization and financial reporting, where clarity is key. Replit is an AI-powered development platform that comes with all Python dependencies pre-installed, so you can skip setup and start coding instantly.
🌐
Python Guides
pythonguides.com › convert-a-string-to-a-double-in-python
How To Convert Python String To Double?
April 2, 2025 - I needed to convert Python string to double for further calculations like total revenue, average order value, etc. After researching various methods, I found several important methods to accomplish this task. I will share my findings in this article with examples. ... Before getting into the methods, it’s important to clarify that Python doesn’t have a specific “double” data type like Java or C++. Instead, Python uses the float type, which is equivalent to a double in other programming languages.
🌐
FavTutor
favtutor.com › blogs › convert-string-to-double-python
Convert String to Double in Python (3 Easy Methods)
October 27, 2022 - Note the produced TypeError also depicts that the operation (generally performed with int or floats) isn't supported by your input format. Hence the need to convert strings to other data types arises. There are many methods for Type Conversion of String to Double in python.
🌐
W3Schools
w3schools.com › python › python_numbers.asp
Python Numbers
x = 1 # int y = 2.8 # float z = 1j # complex #convert from int to float: a = float(x) #convert from float to int: b = int(y) #convert from int to complex: c = complex(x) print(a) print(b) print(c) print(type(a)) print(type(b)) print(type(c)) Try it Yourself » · Note: You cannot convert complex numbers into another number type. Python does not have a random() function to make a random number, but Python has a built-in module called random that can be used to make random numbers:
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.15.2 documentation
The following sections describe the standard types that are built into the interpreter. The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions. Some colle...