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
🌐
W3Schools
w3schools.com › python › ref_func_round.asp
Python round() Function
The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals. The default number of decimals is 0, meaning that the function will return the nearest integer.
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
Why does round(2.35, 1) return 2.4 (rounded up) but round(2.25, 1) returns 2.2 (rounded down)?
It’s a rounding method to balance error. If you always round up, it creates unbalanced weighting towards increasing. The idea is the digit in front of the 5 dictates the direction, odd rounds up while even rounds down. More on reddit.com
🌐 r/learnpython
50
237
March 2, 2020
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
Pythonic way to show 2 decimal places in f-strings?

Do the second one, just don’t worry about rounding it. Let the formatting do the rounding for display.

More on reddit.com
🌐 r/learnpython
10
1
January 13, 2018
🌐
DataCamp
datacamp.com › tutorial › python-round-to-two-decimal-places
How to Round to 2 Decimal Places in Python | DataCamp
August 8, 2024 - # Import the decimal module from decimal import Decimal # Example number to be rounded number = Decimal("18.73869") # Define the rounding precision to 2 decimal places precision = Decimal('0.01') # Using the quantize method with ROUND_UP # to round the number up to 2 decimal places rounded_number ...
🌐
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!
First of all you have to import ... print(maths) OUTPUT: 32909.64 🤓 ... Yes, you can use the round() function in Python to round a float to a specific number of decimal places....
🌐
Codecademy
codecademy.com › article › rounding-to-two-decimal-places-in-pythonn
Rounding to Two Decimal Places in Python | Codecademy
In accordance with standard rounding procedures in financial computations, ROUND_HALF_UP guarantees that numbers precisely halfway between two values round up to the larger value, while Decimal(‘0.01’) indicates that we wish to round to cents. In addition to the decimal module, for controlling numerical precision, Python offers string formatting. We will explore different string formatting methods, including f-strings, str.format(), and the % .2f approach, and discuss them in detail in the next section.
🌐
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.
🌐
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 - At the 3rd decimal place is 5, and the number that comes after it is 4. Since 4 is not greater than 5, the number 5 stays the same and 4 is rounded down to 0. For rounded2, the num is rounded up to 2 decimal places.
Find elsewhere
🌐
Real Python
realpython.com › python-rounding
How to Round Numbers in Python – Real Python
December 7, 2024 - Python’s built-in round() function ... To round numbers to specific decimal places, you can use the round() function with a second argument specifying the number of decimals....
🌐
TradingCode
tradingcode.net › python › math › round-decimals
How to round decimal places up and down in Python? • TradingCode
Here’s what that displays: Original values: [22.459, 5.963, 2.335, -1.569, -0.019, -22.3] Rounded up and down to 2 decimals: [22.46, 5.96, 2.33, -1.57, -0.02, -22.3] Rounded up to 2 decimals: [22.46, 5.97, 2.34, -1.56, -0.01, -22.3] Rounded ...
🌐
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 ...
🌐
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 - Creating a custom function like round_to_2_places() gives you full control. This function implements a classic "round half up" method, which is a common alternative to Python's default. It works by first multiplying the number by 100 to shift the decimal point.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.round.html
numpy.round — NumPy v2.1 Manual
The float printing routines use an accurate but much more computationally demanding algorithm to compute the number of digits after the decimal point. Alternatively, Python’s builtin round function uses a more accurate but slower algorithm for 64-bit floating point values: >>> round(56294995342131.5, 3) 56294995342131.5 >>> np.round(16.055, 2), round(16.055, 2) # equals 16.0549999999999997 (16.06, 16.05)
🌐
Codedamn
codedamn.com › news › javascript
JavaScript round a number to 2 decimal places (with examples)
December 11, 2022 - What do you understand with the question that “Round a number to x decimal places?” The answer is just round off the given decimal number to x decimal places. for example, round the 5.678 to 2 decimal places.
🌐
Altcademy
altcademy.com › blog › how-to-round-to-two-decimal-places-in-python
How to round to two decimal places in Python
September 7, 2023 - We told Python to round to two decimal places by passing 2 as the second argument to the round() function. Python's round() function uses "round half to even" rounding, also known as "bankers' rounding."
🌐
Python
docs.python.org › 3 › library › decimal.html
decimal — Decimal fixed-point and floating-point arithmetic
A: The quantize() method rounds to a fixed number of decimal places. If the Inexact trap is set, it is also useful for validation: >>> TWOPLACES = Decimal(10) ** -2 # same as Decimal('0.01')
🌐
Microsoft Learn
learn.microsoft.com › en-us › power-platform › power-fx › reference › function-round
Int, Round, RoundDown, RoundUp, and Trunc functions - Power Platform | Microsoft Learn
The Round, RoundDown, and RoundUp functions round a number to the specified number of decimal places: Round rounds up if the next digit is 5 or higher.
🌐
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.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.round.html
pandas.DataFrame.round — pandas 3.0.1 documentation
For values exactly halfway between rounded decimal values, pandas rounds to the nearest even value (e.g. -0.5 and 0.5 round to 0.0, 1.5 and 2.5 round to 2.0, etc.).
🌐
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 - Use the format() function (It gives back a formatted version of the input value that has been specified by the format specifier) to round the number upto the 2 decimal places by passing the input number, format (upto to the 2 decimals) as arguments to it.