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 - The round() function is Python’s built-in function for rounding float point numbers to the specified number of decimal places. You can specify the number of decimal places to round by providing a value in the second argument. The example below prints 34.15.
🌐
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 ...
🌐
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
Find elsewhere
🌐
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.
🌐
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?
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 ...
🌐
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 ...
🌐
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')
🌐
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.
🌐
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.
🌐
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')
🌐
W3docs
w3docs.com › python
How to round to 2 decimals with Python?
decimal_num = 3.14159265 rounded_num = "{:.2f}".format(decimal_num) print(rounded_num) # 3.14 ... You can also use the python built-in library 'decimal' to perform precise decimal calculations. from decimal import Decimal decimal_num = ...
🌐
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
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 ...