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
How to Round to 2 Decimal Places in Python | DataCamp
August 8, 2024 - Keep in mind that when you use string formatting techniques in Python to round a number, the rounded output is displayed as a string, but the original number remains unchanged. If you do math on the original number, the calculations are based on the unrounded value, which can lead to surprising results. The % operator offers a traditional method of formatting numbers to two decimal places. The % operator allows for creating formatted strings by inserting values into placeholders. In the example below, f indicates the value as a floating-point number while .2 specifies the decimal places to round the number.
Discussions

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 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 19, 2024
How do I represent currency (i.e., rounding to two decimal places) in Python? I just started learning a few days ago, and any explanation I've found of the decimal function is overwhelming at this point. ELI5, plz?

You're thinking about the wrong aspect of the problem. Yes, floating point innaccuracies mean you get a number like 30.00000000000000001, and decimal will fix that - but you can still divide a price and get a value like $3.718. Decimal won't help you there.

What you really want is a way to round the value to 2 decimal places when you print it. That's the only time that it matters, unless you're a bank and you really care about tiny fractions of a cent (in which case you'll use decimal as well as the following advice).

Check out the format function.

price=14.6188
print("The price is: ${:.2f}".format(price))
The price is: $14.62

This function is very powerful and you should get familiar with it.

product="beer"
print("The price of {:} is ${:.2f}".format(product, price))
The price of beer is $14.62
More on reddit.com
🌐 r/learnpython
19
12
September 12, 2012
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
🌐
Codecademy
codecademy.com › article › rounding-to-two-decimal-places-in-pythonn
Rounding to Two Decimal Places in Python | Codecademy
One of the features of Python’s ... halfway between two potentially rounded values. For example, round(0.5) returns 0 and round(1.5) returns 2 as output....
🌐
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 ...
🌐
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.
🌐
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 - ... from decimal import Decimal, getcontext, ROUND_HALF_UP # Step 1: Create a Decimal object from a floating-point number n1 = Decimal('123.4567') # Step 2: Define the rounding context # '0.01' specifies that we want to round to two decimal ...
🌐
AskPython
askpython.com › home › how to round 2 decimals with python?
How to Round 2 Decimals with Python? - AskPython
May 12, 2023 - 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 ...
Find elsewhere
🌐
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 - The function accepts the number and decimal_point as arguments. decimal_point specifies the decimal place that you want to round the number up to. Let's see an example: num = 24.89 rounded = round(num, 1) print(rounded) # 24.9
🌐
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 - ... float() in Python converts a value to a floating-point number (decimal). Syntax: float(value) Examples: float(10) = 10.0, float("3.14") = 3.14 Use cases: Math calculations, data conversion.
🌐
Real Python
realpython.com › python-rounding
How to Round Numbers in Python – Real Python
December 7, 2024 - You can use the built-in round() function, passing the number and the desired decimal places as arguments, for example, round(number, 2) to round to two decimal places. How do you apply different rounding strategies in Python?Show/Hide
🌐
TradingCode
tradingcode.net › python › math › round-decimals
How to round decimal places up and down in Python? • TradingCode
For example, currency is best reported with 2 decimal digits. And error levels and significance often use more digits, like 5. ... The round() function rounds decimal places up and down.
🌐
GitHub
gist.github.com › jackiekazil › 6201722
How do I round to 2 decimals in python? · GitHub
Python 3.5.4rc1 (default, Jul 25 2017, 08:53:34) [GCC 6.4.0 20170704] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from decimal import Decimal, ROUND_HALF_UP >>> val = Decimal('1.45') >>> Decimal(val.quantize(Decimal('0.1'), rounding=ROUND_HALF_UP)) Decimal('1.5') >>> Decimal(1.45) - Decimal('1.45') Decimal('-4.440892098500626161694526672E-17')
🌐
Inspector
inspector.dev › home › round up numbers to integer in python – fast tips
Round Up Numbers to Integer in Python - Inspector.dev
June 17, 2025 - # Example 2: Rounding to a Specific Decimal Place num = 3.14159 rounded_num = round(num, 2) # Round to 2 decimal places print(rounded_num) # Output: 3.14 · Python provides different rounding methods to handle cases where the fractional part ...
🌐
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 - In order to round a value up, you need to use a custom process. This is often done in finance or accounting, where values are rounded up, regardless of their value. For example, $3.221 would be rounded to $3.23.
🌐
Java2Blog
java2blog.com › home › python › how to round to 2 decimals in python
How to round to 2 decimals in Python - Java2Blog
May 21, 2021 - If we use the floor() function then the final result will get rounded down. It will get rounded up with the ceil() function. We will implement the logic for two user-defined functions to round decimal places in the following code snippet. ... That’s all about how to round to 2 decimal places in Python.
🌐
Programiz
programiz.com › python-programming › methods › built-in › round
Python round()
from decimal import Decimal # normal float num = 2.675 · print(round(num, 2)) # using decimal.Decimal (passed float as string for precision) num = Decimal('2.675')
🌐
TutorialsPoint
tutorialspoint.com › 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 - Input floating-point number: 35.65782 Rounding 4.56782 upto 2 decimal places: 4.57 · In this article, we learned how to round off a given floating number to two decimal places in Python using four different methods. Using the ceil function and some mathematical logic, we learned how to round ...
🌐
Replit
replit.com › home › discover › how to round to 2 decimal places in python
How to round to 2 decimal places in Python
February 6, 2026 - While built-in functions and formatting are great for straightforward cases, you'll sometimes need more powerful tools for financial precision or bulk data operations. from decimal import Decimal, ROUND_HALF_UP number = Decimal('3.14159') rounded ...
🌐
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 - Floating-point numbers are written with a decimal point separating the integer part and the fractional part. Here are some examples of float values- 1.23. 1.08, 45.60, 45.6123004, 1.23e5 (This represents ...