Skip to main content

How to Get Two Decimal Places in Python

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

# Rounding: does not add trailing zeros
print(round(value, 2)) # 3.1 (not 3.10)

# Formatting: preserves trailing zeros
print(f"{value:.2f}") # 3.10

Output:

3.1
3.10
tip

If you need to display exactly two decimal places (including trailing zeros like 3.10 or 5.00), use string formatting. If you just need to round the value for calculations, use round().

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
Banker's Rounding

Python's round() uses banker's rounding (round half to even), which means 0.125 rounds to 0.12 (not 0.13) and 0.135 rounds to 0.14. This reduces cumulative rounding bias in large datasets.

print(round(0.125, 2))  # 0.12 (rounds to even)
print(round(0.135, 2)) # 0.14 (rounds to even)
print(round(0.145, 2)) # 0.14 (rounds to even)
print(round(0.155, 2)) # 0.16 (rounds to even)

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

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

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

# Multiple values
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

# Floating-point imprecision example
print(f"Float: {0.1 + 0.2}") # 0.30000000000000004

# Decimal avoids this
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
tip

Notice that 0.125 rounds to 0.13 with ROUND_HALF_UP, unlike Python's round() which gives 0.12 (banker's rounding). For financial applications, ROUND_HALF_UP is typically the expected behavior.

Available Rounding Modes

ModeBehavior2.5 ->3.5 ->
ROUND_HALF_UPStandard rounding34
ROUND_HALF_EVENBanker's rounding24
ROUND_HALF_DOWNRound half toward zero23
ROUND_UPAlways round away from zero34
ROUND_DOWNAlways round toward zero (truncate)23
ROUND_CEILINGRound toward +inf34
ROUND_FLOORRound toward -inf23

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

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

import math

value = 3.14959

# Truncate to 2 decimal places (round toward zero)
truncated = math.floor(value * 100) / 100
print(f"Truncated: {truncated}")

# Round up to 2 decimal places (round away from zero)
ceiled = math.ceil(value * 100) / 100
print(f"Ceiled: {ceiled}")

Output:

Truncated: 3.14
Ceiled: 3.15
Floating-Point Precision

Be careful with this approach; floating-point multiplication can introduce imprecision:

import math

value = 2.675
print(value * 100) # 267.49999999999997 (not 267.5!)
print(math.ceil(value * 100) / 100) # 2.68 (might expect 2.68, but only by luck)
print(math.floor(value * 100) / 100) # 2.67 (expected 2.67, but the reasoning is fragile)

For precise truncation, use the decimal module instead.

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}")

# With formatting for display
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

MethodTrailing ZerosReturnsBest For
round(x, 2)❌ NofloatQuick rounding for calculations
f"{x:.2f}"✅ YesstrDisplay formatting
"{:.2f}".format(x)✅ YesstrDisplay formatting (older Python)
"%.2f" % x✅ YesstrLegacy code
Decimal.quantize()✅ YesDecimalFinancial/precise calculations
math.floor(x*100)/100❌ NofloatTruncation (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.