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
🌐
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!
Here's an example: number = 10.1234567 rounded_number = round(number, 2) print(rounded_number) # Output: 10.12 Alternatively, you can use the format() function to format the number as a string with 2 decimal places: number = 10.1234567 ...
Discussions

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
January 24, 2024
python - Limiting floats to two decimal points - Stack Overflow
If you know what you are doing, ... of floating point arithmetic and python's decimal class, you might use decimal. But it depends much of your problem. Do you need arbitrary precision decimals? Or only two digits? If two digits: integer. It keeps you out of trouble. Source I worked in a software consultancy for banking. ... I'm coming probably too late here, but I wanted to ask, have the developers of Python solved this problem? Because when I do round(13.949999999999999, 2), I simply ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to round each item in a list of floats to 2 decimal places? - Stack Overflow
I have a list which consists of float values but they're too detailed to proceed. I know we can shorten them by using the ("%.f" % variable) operator, like: result = [359.70000000000005] result = ... 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
🌐
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.
🌐
W3Schools
w3schools.com › python › ref_func_round.asp
Python round() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals.
🌐
DataCamp
datacamp.com › tutorial › python-round-to-two-decimal-places
How to Round to 2 Decimal Places in Python | DataCamp
August 8, 2024 - You will also find the Python Cheat Sheet for Beginners useful as a quick reference if you need a refresher on Python syntax and functions. The simplest method to round a number to two decimal places in Python is by using the built-in round() function, which is pretty straightforward.
🌐
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 ...
Find elsewhere
Top answer
1 of 16
2330

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 ...
🌐
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.
🌐
Tutorialdeep
tutorialdeep.com › knowhow › python faqs › how to round float to 2 decimal places in python
How to Round Float To 2 Decimal Places in Python
December 23, 2024 - In this tutorial, learn how to round float to 2 decimal places in Python. The short answer is: use Python round() to change to 2 decimal places. The two decimal
🌐
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 ...
🌐
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.
🌐
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 - To limit a float to two decimal points use the Python round() function, you simply need to pass the float as the first argument and the value 2 as the second argument.
🌐
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) ...
🌐
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.
🌐
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
🌐
Studytonight
studytonight.com › python-howtos › how-to-round-floating-value-to-two-decimals-in-python
How to Round Floating Value to Two Decimals in Python - Studytonight
January 21, 2021 - We can also use % instead of format() function to get formatted output. It is similar to the format specifier in the print function of the C language. Just use the formatting with %.2f which gives you round down to 2 decimal points.