You can use the round function, which takes as its first argument the number and the second argument is the precision after the decimal point.

In your case, it would be:

answer = str(round(answer, 2))
Answer from rolisz on Stack Overflow
🌐
DataCamp
datacamp.com › tutorial › python-round-to-two-decimal-places
Round to 2 Decimal Places in Python: round(), f-strings & More | DataCamp
August 8, 2024 - Round numbers to 2 decimal places in Python using round(), f-strings, str.format(), the decimal module, and NumPy. Code examples with explanations.
Discussions

python - Limiting floats to two decimal points - Stack Overflow
This is what I mean: If numvar=12.456, ... but "{:2i}".format(numvar) gives an error and I'm expecting 12. 2013-12-12T15:47:04.05Z+00:00 ... Save this answer. ... Show activity on this post. You can use format operator for rounding the value up to two decimal places in Python... More on stackoverflow.com
🌐 stackoverflow.com
how to limit or round a float to only two decimals without rounding up
You can try the solution here: https://stackoverflow.com/a/62435913 More on reddit.com
🌐 r/learnpython
12
5
March 8, 2024
How to round a whole number to 2 decimal places
https://docs.python.org/3/tutorial/inputoutput.html Depending on what formatting style you prefer, it’s going to look something like: number = 56 print(f”{number:.2f}”) More on reddit.com
🌐 r/learnpython
3
3
May 24, 2021
How to format a number with comma and 2 decimal?
Did you try {{ "{:,.2f}".format(data[0]['amount'] }}? The very fine docs at https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.format say In most cases it should be more convenient and efficient to use the % operator or str.format(). More on reddit.com
🌐 r/flask
2
1
November 5, 2021
People also ask

Q2. What if I feel like rounding to more than two decimal places?
Take an example using round(value, 3), here you can just change the number in the rounding parameter.
🌐
intellipaat.com
intellipaat.com › home › blog › how to round-floating value to two decimal places in python
How to Round-Floating Value to Two Decimal Places in Python
Q4. Can we round a list of floats?
Yes, in this case, you can implement list comprehension like [round(x, 2) for x in float_list] to round a list of floats.
🌐
intellipaat.com
intellipaat.com › home › blog › how to round-floating value to two decimal places in python
How to Round-Floating Value to Two Decimal Places in Python
Q3. Is the Decimal module slower than the other methods?
Yes, it is a bit slower than the other methods, but it is worth the time as it is precise and accurate in rounding off, when used in sensitive calculations.
🌐
intellipaat.com
intellipaat.com › home › blog › how to round-floating value to two decimal places in python
How to Round-Floating Value to Two Decimal Places in Python
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-round-floating-value-to-two-decimals-in-python
How to Round Floating Value to Two Decimals in Python - GeeksforGeeks
July 23, 2025 - In Python, we can use the decimal module to round floating-point numbers to a specific number of decimal places with higher precision and control compared to the built-in floating-point arithmetic.
🌐
Codecademy
codecademy.com › article › rounding-to-two-decimal-places-in-pythonn
Rounding to Two Decimal Places in Python | Codecademy
In Python, we can use the built-in round function to round floating-point numbers to a specified number of decimal places. One of the features of Python’s round() function is its implementation of “round half to even,” also known as ...
Top answer
1 of 16
2332

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
🌐
Analytics Vidhya
analyticsvidhya.com › home › 6 ways to round floating value to two decimals in python
6 Ways to Round Floating Value to Two Decimals in Python
November 4, 2024 - The built-in Python function round() can round a floating-point number to an integer or to a specified number of decimal places. You can specify the number of decimal places you want and the number you want to round using two different ...
Find elsewhere
🌐
datagy
datagy.io › home › python posts › how to round to 2 decimal places in python
How to Round to 2 Decimal Places in Python • datagy
April 13, 2023 - # Rounding a Value to 2 Decimal Places in Python value = 1.2345 rounded = round(value, 2) print(rounded) # Returns: 1.23 · In the following section, you’ll learn how to round up a float to 2 decimal places.
🌐
Sololearn
sololearn.com › en › Discuss › 3288381 › how-to-round-to-2-decimal-places-in-python
How to round to 2 decimal places in python
sl_scroll_/de/Discuss/2689918/i-made-a-bmi-calculator-in-python-and-youa-are-supposed-to-write-the-height-in-meter-i-want-whenever-you-write-the-height-in-cePending
🌐
Mimo
mimo.org › glossary › python › round-function
Mimo: The coding platform you need to learn Web Development, Python, and more.
... from decimal import Decimal, ROUND_HALF_UP # Create Decimal objects instead of using floats num = Decimal('2.675') # Use the quantize() method to round to two decimal places with rounding mode ROUND_HALF_UP rounded_num = num.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) print(rounded_num) ...
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-round-down-to-2-decimals-a-float-using-python
How to round down to 2 decimals a float using Python?
August 29, 2023 - Pass input number, 2 (decimal value) as arguments to the round() function to round the input number upto the 2 decimal places. Print the rounded value of the input floating-point number upto 2 decimals.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-round-to-2-decimal-places-in-python
How to Round to 2 Decimal Places in Python
August 22, 2022 - For rounded2, the num is rounded up to 2 decimal places. At the 2nd decimal place is 4, and the number after it is 5. Since this number is greater than or equal to 5, the number 4 is rounded up to 5. Rounding up numbers can be useful for keeping floating numbers within fixed digits.
🌐
GitHub
gist.github.com › jackiekazil › 6201722
How do I round to 2 decimals in python? · GitHub
from decimal import Decimal # First we take a float and convert it to a decimal x = Decimal(16.0/7) # Then we round it to 2 places output = round(x,2) print output
🌐
AskPython
askpython.com › home › how to round 2 decimals with python?
How to Round 2 Decimals with Python? - AskPython
May 12, 2023 - Now let us get on with another technique that can be used to round the decimal places to 2. The format( ) function makes use of the format specification operand ‘:’ to set the precision of the decimal places. Let us have a look at how it is done. ip = 2.1258908 op = format(ip,".2f") print("Rounded number:", op) Rounded number: 2.13 ... Rather than a function, this time it is a module which ought to be imported from the Python library using the following code.
🌐
PythonHow
pythonhow.com › how › limit-floats-to-two-decimal-points
Here is how to limit floats to two decimal points in Python
In this case, the float x is rounded to two decimal points, resulting in the value 3.14.Alternatively, you can use the format function to format a float as a string with a fixed number of decimal points. Here is an example of how to use format to limit a float to two decimal points: x = 3.14159265 # Format x as a string with two decimal points y = "{:.2f}".format(x) print(y) # Output: "3.14"The format function takes a format string as the first argument and the value to format as the second argument.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to round float to 2 decimals in python
5 Best Ways to Round Float to 2 Decimals in Python - Be on the Right Side of Change
February 25, 2024 - In the code above, the placeholder {:.2f} within the string defines the format, indicating that the float should be formatted with two decimal places. The format method then replaces the placeholder with the rounded number. The Decimal module in Python provides arithmetic with as much precision as needed.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python limit floats to two decimal points
Python Limit Floats to Two Decimal Points - Spark By {Examples}
May 31, 2024 - How to limit floats to two decimal points in Python? One solution to limit floats to two decimal points is to use the round() function, which rounds a
🌐
Ceos3c
ceos3c.com › home › python › python round 2 decimals with floats – super easy method
Python Round 2 Decimals with Floats - Super Easy Method
October 5, 2022 - There are a few different ways to round numbers in Python. The first method we’ll discuss is the built-in round() function. This function takes two arguments: the number you want to round and the number of decimal places you want to round it to. For example, if we wanted to round the number 4.738 to two decimal places, we would write: ... This is the easiest, most straightforward way to round 2 decimals with Floats in Python.
🌐
Sentry
sentry.io › sentry answers › python › limit floats to two decimal points in python
Limit floats to two decimal points in Python | Sentry
March 15, 2023 - We can do this using Python’s round() function, which rounds a given number to a specified number of decimal places. For example: my_number = 9.489999999999 rounded = round(my_number, 2) print(rounded) # will output "9.49"
🌐
Intellipaat
intellipaat.com › home › blog › how to round-floating value to two decimal places in python
How to Round-Floating Value to Two Decimal Places in Python
July 25, 2025 - Yes, it is a bit slower than the ... list of floats? Yes, in this case, you can implement list comprehension like [round(x, 2) for x in float_list] to round a list of floats....