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
🌐
GeeksforGeeks
geeksforgeeks.org › python › precision-handling-python
Precision Handling in Python - GeeksforGeeks
December 19, 2025 - By setting getcontext().prec, every Decimal calculation automatically respects that precision limit. Python · from decimal import Decimal, getcontext getcontext().prec = 3 r = Decimal('3') / Decimal('9') print(r) Output · 0.333 · Explanation: getcontext().prec = 3 sets the total number of significant digits allowed.
Discussions

Is there a way to limit the number of digits in an integer with Python?
Write a program that prompts the user to enter a two-digit integer . This was the question and I tried to work it like this unsuccessfully. def main(): # Prompt user to enter integer numb1 = int(input('Enter a two digit integer ')) # Verify integer entered is correct before continuing if ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
January 28, 2022
python - How to limit the number of digits? - Stack Overflow
What should I add to my code to make return value 15.58, without using any library ? def solution(side): result = (side**2)*(3**(0.5))/4 return result # return = 15.5885 More on stackoverflow.com
🌐 stackoverflow.com
python - How can I limit the amount of digits in an input? - Stack Overflow
(Also I understand that there is ... a fairly limited knowledge of python) ... aa = (a*11) and bb = (b*10) are totally bogus, they force user to input one single digit then multiply whatever that digit is by 10 or 11; instead of iterating 10 or 11 times on the call to input() ... You have to convert the int to a string because int does not have a length property. Also You were checking if the digit was longer than 1 for a twice so I switched the SECOND NUMBER check to b · print('Please enter ... More on stackoverflow.com
🌐 stackoverflow.com
python - How do I limit an output's maximum number of characters? - Stack Overflow
I'm trying to limit a number's output to 4 characters (including decimals). def costEdit(nCost): nCost = '%.2f'%(nCost*1.25) nCost = nCost * 1.25 return nCost I want the nCost to be cu... More on stackoverflow.com
🌐 stackoverflow.com
🌐
freeCodeCamp
forum.freecodecamp.org › python
Is there a way to limit the number of digits in an integer with Python? - Python - The freeCodeCamp Forum
January 28, 2022 - Write a program that prompts the user to enter a two-digit integer . This was the question and I tried to work it like this unsuccessfully. def main(): # Prompt user to enter integer numb1 = int(input('Enter a two digit integer ')) # Verify integer entered is correct before continuing if len(numb1)!= 2: print(f'Something is wrong with the first input.
🌐
Python documentation
docs.python.org › 3 › tutorial › floatingpoint.html
15. Floating-Point Arithmetic: Issues and Limitations — Python 3.14.4 documentation
On most machines, if Python were to print the true decimal value of the binary approximation stored for 0.1, it would have to display: >>> 0.1 0.1000000000000000055511151231257827021181583404541015625 · That is more digits than most people find useful, so Python keeps the number of digits manageable by displaying a rounded value instead:
🌐
Python Engineer
python-engineer.com › posts › limit-float-python
How to limit float values to N decimal places in Python - Python Engineer
June 20, 2022 - This article will show how we can limit a float number to N decimal places using a format specifier. Format specifiers define how data is to be printed on standard output, it includes doing operations like truncating values and extending values.
Find elsewhere
🌐
Quora
quora.com › How-do-you-limit-decimal-places-in-Python
How to limit decimal places in Python - Quora
In that case, string formatting can help you out: [code]In [6]: x = 0.406 In [7]: f'{x:.2f}' Out[7]: '0.41' [/code]The [code ].2[/code] in the format specifier indicates we want 2 ...
🌐
CodeGenes
codegenes.net › blog › how-to-limit-decimal-places-in-python
How to Limit Decimal Places in Python — codegenes.net
num = 3.7 rounded_to_int = round(num) print(rounded_to_int) # Output: 4 · Python provides several ways to format strings that can be used to limit decimal places.
Top answer
1 of 4
4

The problem you're having is confusion between a number, and a string representation of a number. These are different things.

When you do this:

nCost = '%.2f'%(nCost*1.25)

… you're multiplying your number by 1.25, then replacing it with a string that represents the number up to two decimal points. So, when you do this:

    nCost = nCost * 1.25

… you're trying to multiply a string by 1.25, which makes no sense.


I suspect you didn't want a string here at all; you just wanted to round the number as a number. To do that, use the round function:

nCost = round(nCost*1.25, 2)

Of course as soon as you multiply by 1.25 again, you may "unround" it by another decimal place or two:

>>> nCost = 1.33333
>>> nCost = round(nCost*1.25, 2)
>>> nCost
1.33
>>> nCost = nCost * 1.25
>>> nCost
1.6625

And really, it's almost always a bad idea to round values before doing arithmetic on them; that just multiplies rounding errors unnecessarily. It's better to just keep the highest-precision value around as long as possible, and round at the last possible second—ideally by using %.2f/{:.2f} formatting in the printing/writing-to-disk/etc. stage.


Another thing to keep in mind is that not all real numbers can be represented exactly as floats. So, for example, round(13.4999, 2) is actually 13.949999999999999289457264. A simple repr or str or %f will print this out as 13.95, so you may not notice… but it's still not exactly 13.95.

The best way to solve this problem, and your other problems, is to use the Decimal type instead of the float type. Then you can round the value to exactly 2 decimal places, do math in a 2-decimal-places context, etc.

2 of 4
2

One way to do this would be with the following:

def costEdit(nCost):
    nCost = '%.2f'%(nCost*1.25)
    #nCost = nCost*1.25
    return nCost

Let me break this down for you.

Take the line:

'%.2f'%(nCost*1.25)

And break it down into its components:

(' ( (%) .2f) ')%(nCost*1.25)
  • Everything inside '' is just a normal string.
  • The % is a symbol that means "substitute in later".
  • .2f is a command in Python that truncates 2 places after a decimal.
  • %(nCost*1.25) means "nCost*1.25 is what you put in place of %"

So in order, it does the math of nCost*1.25, puts it inside the string, then cuts off everything else and gives you what is left.

Take note that I've rewritten your costEdit function to accept nCost as an argument. This means that now you can call:

costEdit(nCost)

with any number that you want, and it will do the math.

Also note I've replaced print with return. This means that now, you will have to write:

print costEdit(80)

to get

100

However it now means you can do math with costEdit, such as:

print 4*costEdit(80)

and instead of printing 100 along the way, it will just print

400

Questions? Leave a comment.

Happy coding!

EDIT: Regarding your error,

I think I've reproduced it and understand the problem.

When I type:

costEdit('15')

I get your error.

This is because when you do costEdit('15'), you're entering '15' and not 15. If a value is between '' or "", you're dealing with a value of type string.

In python, you can't multiply a float (type of number) with a string. That's what this is complaining about.

To remedy the situation, just remove the quotes. Then you're entering a number of type float or int (integer), both of which can be multiplied by a float (the value 1.25 inside costEdit()).

I hope this solves your issue-- I have to get off the PC for the night. If it doesn't help, leave a comment and I'll return tomorrow.

Otherwise, I'm glad I could help and remember to select a best answer on your question, so those who helped can get their precious precious pointssssss

🌐
Scientifically Sound
scientificallysound.org › 2016 › 10 › 10 › python-print2
Take control of your Python print() statements: part 2 | Scientifically Sound
October 10, 2016 - Pi with 6 total digits, including 5 digits after the decimal point 3.14159 Pi with 4 total digits, including 1 digits after the decimal point 3.1 Pi with 8 total digits, including 1 digits after the decimal point 3.1 · Notice something odd about the last print statement? We have asked Python to print pi using 8 spaces, but allowing for only 1 digit after the decimal point.
🌐
The Teclado Blog
blog.teclado.com › python-formatting-numbers-for-printing
Formatting Numbers for Printing in Python - The Teclado Blog
March 23, 2023 - We can use f on its own as well, which defaults to 6 digits of precision: ... For large numbers we often write a separator character (usually a comma, space, or period) to make it easier to read.
🌐
Stack Overflow
stackoverflow.com › questions › 74292290 › python-limit-the-maximum-amount-of-decimals-in-all-print-calls
numpy - Python: Limit the maximum amount of decimals in all print calls - Stack Overflow
I have a script with a lot of print calls. I want to limit all the printed numbers to a maximum of 5 decimals, but changing it by hand with f_strings or formatting would be a lot of work. Is there ...
🌐
Post.Byes
post.bytes.com › home › forum › topic › python
How to limit digits after decimal point? - Post.Byes
Hello guys, How would I control digits after decimal point? Andy I assume that you are referring to display of output. Format strings will do it. To format a float with a precision of four decimal places: ... I assume that you are referring to display of output. Format strings will do it. To format a float with a precision of four decimal places: ... >>> a = 123.123456 >>> print "%0.4f" % (a) 123.1235 To all who read this: String formating is an essential tool in almost any app.
🌐
Python Guides
pythonguides.com › python-print-2-decimal-places
How to Print Two Decimal Places in Python
December 22, 2025 - While I don’t use it in new Python projects, you should understand how it works if you are maintaining legacy code for a US client. # Percent of US households with a pet pet_percentage = 66.12345 # Formatting using the % operator print("Pet ownership in the USA: %.2f%%" % pet_percentage) # Output: Pet ownership in the USA: 66.12%