You are running into the old problem with floating point numbers that not all numbers can be represented exactly. The command line is just showing you the full floating point form from memory.

With floating point representation, your rounded version is the same number. Since computers are binary, they store floating point numbers as an integer and then divide it by a power of two so 13.95 will be represented in a similar fashion to 125650429603636838/(2**53).

Double precision numbers have 53 bits (16 digits) of precision and regular floats have 24 bits (8 digits) of precision. The floating point type in Python uses double precision to store the values.

For example,

>>> 125650429603636838/(2**53)
13.949999999999999

>>> 234042163/(2**24)
13.949999988079071

>>> a = 13.946
>>> print(a)
13.946
>>> print("%.2f" % a)
13.95
>>> round(a,2)
13.949999999999999
>>> print("%.2f" % round(a, 2))
13.95
>>> print("{:.2f}".format(a))
13.95
>>> print("{:.2f}".format(round(a, 2)))
13.95
>>> print("{:.15f}".format(round(a, 2)))
13.949999999999999

If you are after only two decimal places (to display a currency value, for example), then you have a couple of better choices:

  1. Use integers and store values in cents, not dollars and then divide by 100 to convert to dollars.
  2. Or use a fixed point number like decimal.
Answer from Rex Logan on Stack Overflow
Top answer
1 of 16
2331

You are running into the old problem with floating point numbers that not all numbers can be represented exactly. The command line is just showing you the full floating point form from memory.

With floating point representation, your rounded version is the same number. Since computers are binary, they store floating point numbers as an integer and then divide it by a power of two so 13.95 will be represented in a similar fashion to 125650429603636838/(2**53).

Double precision numbers have 53 bits (16 digits) of precision and regular floats have 24 bits (8 digits) of precision. The floating point type in Python uses double precision to store the values.

For example,

>>> 125650429603636838/(2**53)
13.949999999999999

>>> 234042163/(2**24)
13.949999988079071

>>> a = 13.946
>>> print(a)
13.946
>>> print("%.2f" % a)
13.95
>>> round(a,2)
13.949999999999999
>>> print("%.2f" % round(a, 2))
13.95
>>> print("{:.2f}".format(a))
13.95
>>> print("{:.2f}".format(round(a, 2)))
13.95
>>> print("{:.15f}".format(round(a, 2)))
13.949999999999999

If you are after only two decimal places (to display a currency value, for example), then you have a couple of better choices:

  1. Use integers and store values in cents, not dollars and then divide by 100 to convert to dollars.
  2. Or use a fixed point number like decimal.
2 of 16
838

There are new format specifications, String Format Specification Mini-Language:

You can do the same as:

"{:.2f}".format(13.949999999999999)

Note 1: the above returns a string. In order to get as float, simply wrap with float(...):

float("{:.2f}".format(13.949999999999999))

Note 2: wrapping with float() doesn't change anything:

>>> x = 13.949999999999999999
>>> x
13.95
>>> g = float("{:.2f}".format(x))
>>> g
13.95
>>> x == g
True
>>> h = round(x, 2)
>>> h
13.95
>>> x == h
True
🌐
Python
docs.python.org › 3 › library › decimal.html
decimal — Decimal fixed-point and floating-point arithmetic
Source code: Lib/decimal.py The decimal module provides support for fast correctly rounded decimal floating-point arithmetic. It offers several advantages over the float datatype: Decimal “is based...
Discussions

How to convert int to float in python? - Stack Overflow
Does anyone know how to convert int to float. For some reason, it keeps on printing 0. I want it to print a specific decimal. sum = 144 women_onboard = 314 proportion_womenclass3_survived = sum / n... More on stackoverflow.com
🌐 stackoverflow.com
How to use the float() command to create 2 decimal places instead of one in a product of 2 numbers [the product is dollar amount]
You generally don't round until you go to display the value. You can use round for that, but since you just want to display it with two digits, you can use string formatting . In your case, something like print(f'{totalPay:.2f}'). .2f is the format specifier, saying round the float (f) to two decimal places (.2). More on reddit.com
🌐 r/learnpython
9
7
January 28, 2020
python - How to display a float with two decimal places? - Stack Overflow
I have a function taking float arguments (generally integers or decimals with one significant digit), and I need to output the values in a string with two decimal places (5 → 5.00, 5.5 → 5.50, etc)... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
Sololearn
sololearn.com › en › Discuss › 3288381 › how-to-round-to-2-decimal-places-in-python
How to round to 2 decimal places in python | Sololearn: Learn to code for FREE!
In your case, you can use the round() function to round the result of your calculation to 2 decimal places, like this: total_cost = round((price * quantity) * (1 - discount) * (1 + tax), 2) This will give you the total cost with 2 decimal places, ...
🌐
Quora
quora.com › How-do-you-convert-a-decimal-to-a-float-in-Python
How to convert a decimal to a float in Python - Quora
To convert a Decimal (from the decimal module) to a float in Python, use the built-in float() constructor. That produces a binary floating‑point approximation of the Decimal's value.
🌐
TutorialsPoint
tutorialspoint.com › article › How-to-convert-float-to-integer-in-Python
How to convert float to integer in Python?
The int() function takes an integer string or a float, but it does not take a float string. If a float string is given, first we need to convert it to float by using the float() function then apply it to int().
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › convert-int-to-float
Convert int to float - GeeksforGeeks
March 26, 2024 - Converting an integer to a float involves explicitly casting the integer value to a float type. This conversion allows the integer value to be treated as a floating-point number with decimal precision.
🌐
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
🌐
Vultr Docs
docs.vultr.com › python › built-in › float
Python float() - Convert To Floating Point | Vultr Docs
November 22, 2024 - Explore how this function handles ... and empty values. Begin with an integer value. Apply the float() function to convert the integer to a floating-point number....
🌐
LabEx
labex.io › tutorials › python-how-to-convert-between-integers-and-floats-in-python-397961
How to convert between integers and floats in Python | LabEx
## Automatic type conversion print(5 + 3.2) ## Output: 8.2 print(10 / 4) ## Output: 2.5 · Understanding the differences and similarities between integers and floats is essential for writing efficient and accurate Python code.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-convert-integers-to-floats-in-python-3
How To Convert Integers to Floats in Python 3 | DigitalOcean
September 18, 2020 - By using the float() function, we’ve converted an integer to a float. If you’d like to learn more about converting different data types in Python, check out our How To Convert Data Types in Python 3 tutorial.
🌐
Reddit
reddit.com › r/learnpython › float and int function
r/learnpython on Reddit: Float and int function
March 25, 2023 -

Hello, please can someone explain to me when to use the float and int functions?

I mean should I use float when I am strictly dealing with decimal numbers, or when I want to convert whole numbers to decimal numbers?

pythonlearner

Top answer
1 of 4
5
float() and int() are mainly use to convert strings to numbers. They actually create a number from the strings. int() is also often use to create an integer number from a float number. A classic example is when you ask the user for a number. The input function always returns a string of character. To perform calculation you need a number (int or float). So you must create a number from that string. As in: string_number = input("Enter temperature in °F (integer): ") temp_far = int(string_number) temp_cel = (temp_far-32) * (5/9) # the result here is a float print("Temperature in celsius (float)", temp_cel) temp_cel_int = int(temp_cel) # creates an integer number from the float print("Temperature in Celsius (integer)", temp_cel_int) Using float on strings from math import radians angle_string = input("Enter a precise angle in degrees with decimal: ") angle_float = float(angle_string) radians_float = radians(angle_float) print("That angle in degrees is",radians_float,"in radians")
2 of 4
1
For string conversion, use the one appropriate to the string you're trying to convert. If you care about exact equality, don't use floats. Use integers, or maybe the Decimal class. If you want to know if floats are equal ish, keep in mind that the ish is always there. Use something like math.isclose to make such checks. There is rarely (almost never?) any need to explicitly convert integers to floats (unless you're doing something with numpy or similar). Integers will be automatically treated as floats during division (NOTE: different in some other languages), multiplication, etc. If you want to convert a float to integer, you can do that via int(2.3), but keep in mind how it rounds. There are other rounding functions that may or may not be more helpful (math.ceil, math.floor, round).
🌐
AskPython
askpython.com › home › how to format a number to 2 decimal places in python?
How to Format a Number to 2 Decimal Places in Python? - AskPython
February 27, 2023 - Here “{:.2f}” is a string where we have mentioned .2f, which means 2 digits after the decimal in the float number. Then we provide our float value on which the action will take place in the format function. In the result, we are getting the same result using two different ways. You can check The Python float() Method for more knowledge on the float method.
🌐
Flexiple
flexiple.com › python › float-to-int-python
How to convert Float to int in Python? | Flexiple Tutorials | Python - Flexiple
Convert float to int in Python effortlessly. Learn how with our concise guide on float-to-int conversion methods and best practices.
🌐
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
We can also convert the numbers in the example above to float values by using the float() method in place of the int() method. Instead of receiving the output of 58, we’ll receive the output of 58.0, a float. The user Sammy is earning points in decimal values
🌐
Devcamp
basque.devcamp.com › pt-full-stack-development-javascript-python-react › guide › how-to-convert-between-integer-float-decimal-complex-numeric-data-types-python
How to Convert Between the Integer, Float, Decimal and Complex Numeric Data Types in Python
In this section of numbers in Python, we've talked about not only numbers from a high-level perspective and some of the functions associated with them. Such as how to run calculations and elements like that. But we've also talked about the sub data types within numbers such as integers and floats and different elements like that and usually, Python is going to manage the process of converting those for you automatically.
🌐
Replit
replit.com › home › discover › how to convert a float to an int in python
How to convert a float to an int in Python | Replit
February 6, 2026 - This behavior is called truncation—the function simply discards the decimal portion, effectively rounding the number toward zero. It's important to note that this is not mathematical rounding. Whether the float is 10.1 or 10.9, applying int() will always yield 10. This makes the function a predictable and reliable choice when you need to isolate the whole number part of a float without complex rounding rules. When you need more control than simple truncation, Python’s math module and built-in round() function provide more specific ways to convert floats.