Skip to content

How to Get Two Decimal Places in Python

Learn how to round numbers or format them to exactly two decimal places in Python using built-in functions, f-strings, and the decimal module for financial precision.

When working with financial calculations, scientific data, or any application that requires precise number formatting, you'll often need to display or round values to exactly two decimal places. Python provides several ways to accomplish this, from simple built-in functions to specialized modules. In this guide, we'll cover all the common methods and explain when to use each one.

Quick Answer: Use round()

value = 3.14159265359
result = round(value, 2)
print(result)

Output:

3.14

The built-in round() function takes two arguments: the number and the number of decimal places.

Understanding the Difference: Rounding vs. Formatting

Before diving into methods, it's important to understand that there are two different goals:

  1. Rounding a number: changing the actual value (for example, 3.14159 -> 3.14).
  2. Formatting for display: controlling how the number appears as a string (for example, 3.1 displayed as "3.10").
value = 3.1

print(round(value, 2))    # 3.1 (not 3.10)

print(f"{value:.2f}")     # 3.10

Output:

3.1
3.10

Method 1: Using round()

The simplest way to round a number to two decimal places:

values = [3.14159, 2.71828, 1.0, 9.999, 0.125]

for v in values:
    print(f"{v} -> {round(v, 2)}")

Output:

3.14159 -> 3.14
2.71828 -> 2.72
1.0 -> 1.0
9.999 -> 10.0
0.125 -> 0.12

Method 2: Using f-Strings (Python 3.6+)

F-strings offer the most readable way to format numbers with two decimal places:

price = 19.9
tax = 1.5983
total = price + tax

print(f"Price: ${price:.2f}")
print(f"Tax:   ${tax:.2f}")
print(f"Total: ${total:.2f}")

Output:

Price: $19.90
Tax:   $1.60
Total: $21.50

The :.2f format specifier means "format as a float with 2 decimal places."

Formatting Options with f-Strings

value = 1234.5

print(f"Two decimals:       {value:.2f}")      # 1234.50
print(f"With commas:        {value:,.2f}")     # 1,234.50
print(f"Right-aligned (15): {value:>15.2f}")   #         1234.50
print(f"With sign:          {value:+.2f}")     # +1234.50
print(f"Zero-padded (12):   {value:012.2f}")   # 000001234.50

Output:

Two decimals:       1234.50
With commas:        1,234.50
Right-aligned (15):         1234.50
With sign:          +1234.50
Zero-padded (12):   000001234.50

Method 3: Using format() Method

The str.format() method works the same way as f-strings but uses placeholder syntax:

value = 3.14159

print("{:.2f}".format(value))

print("Pi is approximately {pi:.2f}".format(pi=value))

print("Width: {:.2f}, Height: {:.2f}".format(10.5, 7.333))

Output:

3.14
Pi is approximately 3.14
Width: 10.50, Height: 7.33

Method 4: Using % Operator

The oldest string formatting approach in Python, still found in legacy code:

value = 3.14159

print("%.2f" % value)
print("Price: $%.2f" % 19.9)

Output:

3.14
Price: $19.90

Method 5: Using the decimal Module (Financial Precision)

For financial calculations where floating-point imprecision is unacceptable, use the decimal module:

from decimal import Decimal, ROUND_HALF_UP

print(f"Float: {0.1 + 0.2}")  # 0.30000000000000004

result = Decimal("0.1") + Decimal("0.2")
print(f"Decimal: {result}")    # 0.3

Output:

Float: 0.30000000000000004
Decimal: 0.3

Rounding to Two Decimal Places with Decimal

from decimal import Decimal, ROUND_HALF_UP

values = ["3.14159", "2.71828", "9.995", "0.125"]

for v in values:
    rounded = Decimal(v).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
    print(f"{v} -> {rounded}")

Output:

3.14159 -> 3.14
2.71828 -> 2.72
9.995 -> 10.00
0.125 -> 0.13

Available Rounding Modes

Mode Behavior 2.5 -> 3.5 ->
ROUND_HALF_UP Standard rounding 3 4
ROUND_HALF_EVEN Banker's rounding 2 4
ROUND_HALF_DOWN Round half toward zero 2 3
ROUND_UP Always round away from zero 3 4
ROUND_DOWN Always round toward zero (truncate) 2 3
ROUND_CEILING Round toward +inf 3 4
ROUND_FLOOR Round toward -inf 2 3

Method 6: Using math.floor() and math.ceil()

For truncating (always rounding down) or always rounding up:

import math

value = 3.14959

truncated = math.floor(value * 100) / 100
print(f"Truncated: {truncated}")

ceiled = math.ceil(value * 100) / 100
print(f"Ceiled:    {ceiled}")

Output:

Truncated: 3.14
Ceiled:    3.15

Practical Examples

Formatting Currency

def format_currency(amount, symbol="$"):
    """Format a number as currency with two decimal places."""
    return f"{symbol}{amount:,.2f}"

print(format_currency(1234.5))
print(format_currency(0.5))
print(format_currency(1000000))
print(format_currency(49.99, "€"))

Output:

$1,234.50
$0.50
$1,000,000.00
€49.99

Rounding a List of Numbers

prices = [19.995, 24.504, 9.999, 149.991, 3.497]

rounded = [round(p, 2) for p in prices]
print(f"Original: {prices}")
print(f"Rounded:  {rounded}")

formatted = [f"${p:.2f}" for p in prices]
print(f"Formatted: {formatted}")

Output:

Original: [19.995, 24.504, 9.999, 149.991, 3.497]
Rounded:  [20.0, 24.5, 10.0, 149.99, 3.5]
Formatted: ['$20.00', '$24.50', '$10.00', '$149.99', '$3.50']

Precise Financial Calculation

from decimal import Decimal, ROUND_HALF_UP

subtotal = Decimal("99.99")
tax_rate = Decimal("0.0825")

tax = (subtotal * tax_rate).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
total = subtotal + tax

print(f"Subtotal: ${subtotal}")
print(f"Tax (8.25%): ${tax}")
print(f"Total: ${total}")

Output:

Subtotal: $99.99
Tax (8.25%): $8.25
Total: $108.24

Method Comparison

Method Trailing Zeros Returns Best For
round(x, 2) ❌ No float Quick rounding for calculations
f"{x:.2f}" ✅ Yes str Display formatting
"{:.2f}".format(x) ✅ Yes str Display formatting (older Python)
"%.2f" % x ✅ Yes str Legacy code
Decimal.quantize() ✅ Yes Decimal Financial/precise calculations
math.floor(x*100)/100 ❌ No float Truncation (not rounding)

Conclusion

To get two decimal places in Python, choose the method based on your need:

  • use round(value, 2) for quick rounding in calculations,
  • f-strings (f"{value:.2f}") for display formatting with guaranteed trailing zeros,
  • decimal module with ROUND_HALF_UP for financial applications where precision and predictable rounding behavior are critical.

For most everyday tasks, f-strings provide the cleanest syntax and most readable code.

Navigation