Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Float Rounding: How to Round Correctly

Understand how Python rounds floats, why round() can behave unexpectedly, and when to use Decimal or formatting for precise control.

~5 min readPython
#Python#floating-point#rounding#Decimal
A stylized illustration of a Python float being rounded with a magnifying glass revealing binary digits and a decimal point.

When you write round(2.675, 2) in Python, you get 2.67, not 2.68. That result surprises many developers, but it follows directly from how floating-point numbers are stored in binary. The decimal value 2.675 cannot be represented exactly as a binary fraction, so the underlying float is slightly less than 2.675. The round() function then rounds that imprecise value, producing 2.67. This is the first thing to understand about Python float rounding: the input to round() is already an approximation, so the output reflects that approximation, not the decimal you typed.

How the Built-in round() Actually Works

The built-in round() function uses round-half-to-even, often called banker's rounding. For a number exactly halfway between two candidates, it rounds to the nearest even final digit. For example, round(2.5) returns 2, and round(3.5) returns 4. This behavior is intentional and reduces cumulative bias in statistical calculations, but it can be unexpected in financial or display contexts where you expect round-half-up.

python
print(round(2.5)) # 2 print(round(3.5)) # 4 print(round(2.675, 2)) # 2.67 (due to binary representation)

The optional second argument, ndigits, controls how many digits after the decimal point are kept. When it is negative, rounding applies to tens, hundreds, and so on: round(1234, -2) gives 1200. With no ndigits, round() on a float returns an int; with ndigits supplied, a float input returns a float, even if the result could be an integer. An integer input returns an int in both cases.

Formatting Floats for Display: A Different Rounding Path

String formatting with f-strings or the format() method performs rounding for display purposes, but it does not change the underlying value. The format spec :.2f rounds to two decimal places using the same binary representation, so it can also produce surprising results. However, formatting always returns a string, which is often exactly what you need for output.

python
value = 2.675 print(f"{value:.2f}") # 2.67 print(format(value, ".2f")) # 2.67

Formatting also starts from the same underlying binary float, so it can produce the same surprising result as round() for values like 2.675. It returns a string, so it is suitable for display but does not change the stored value. If you need to round a value before storing it or using it in further calculations, formatting alone is insufficient.

Using decimal.Decimal for Exact Decimal Arithmetic

When you need predictable decimal rounding, the decimal module provides exact representation of decimal numbers and full control over rounding modes. For exact decimal work, create a Decimal from a string or integer rather than from a float, because converting a float to Decimal preserves the binary approximation. The quantize() method rounds to a specified exponent and accepts a rounding mode.

python
from decimal import Decimal, ROUND_HALF_UP value = Decimal("2.675") rounded = value.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) print(rounded) # 2.68

ROUND_HALF_UP rounds ties away from zero, which matches the intuitive rounding taught in most contexts. Other modes include ROUND_CEILING, ROUND_FLOOR, ROUND_DOWN, and ROUND_UP. The active decimal context also sets precision and rounding globally, but quantize() with an explicit mode is the clearest way to control a single operation.

Comparing Rounding Approaches

The following table summarizes the main options for rounding Python floats, their output types, and when each is appropriate.

MethodOutput typeRounding ruleBest for
round(x, n)float/intRound half to evenGeneral numeric work, statistics
f-string / format()stringRound half to evenDisplay and reporting
Decimal.quantize()DecimalConfigurableFinancial calculations, exact decimals
math.floor() / ceil()intDown/up to nearest integerInteger rounding in specific directions

math.floor() and math.ceil() are not rounding in the decimal sense, but they are often used when you need to force a value down or up to the nearest integer. They do not accept a number of digits, so they are limited to integer results.

Precision and Performance Tradeoffs

Using Decimal can be slower than using native floats because decimal arithmetic is implemented in software, not hardware. For a single rounding operation the difference is negligible, but in a tight loop processing millions of values, Decimal can become a bottleneck. For display, formatting is often the simplest approach and avoids creating a Decimal object. If you need exact decimal behavior in financial calculations, the performance cost of Decimal is usually acceptable compared to the risk of incorrect rounding.

Another performance consideration is how a Decimal gets its value. If you are parsing user input, you already have a string, so Decimal("2.675") is natural. If you receive a native float from a sensor or calculation, directly passing it to Decimal preserves the binary approximation, which is rarely what you want for decimal rounding. You may need to round-trip through a string representation, such as Decimal(str(value)), which adds overhead. In such cases, decide whether the rounding error is acceptable for your domain or whether you need the exactness.

Common Pitfalls in Float Rounding

One frequent mistake is assuming round() always rounds half up. The banker's rounding behavior is correct for many statistical applications, but it can cause off-by-one errors in financial reports. Another pitfall is chaining rounding operations: rounding an intermediate result and then rounding again can introduce more error than rounding once at the end. For example, round(round(2.675, 2), 1) gives 2.7, while round(2.675, 1) gives 2.7 as well, but with other numbers the results can diverge.

Accumulated floating-point error is another issue. Adding many small floats can produce a sum that is slightly off, and rounding that sum may not give the expected result. The math.fsum() function provides an accurate sum, but it still returns a float. If you need exact decimal sums, use Decimal throughout the calculation.

Choosing the Right Rounding Strategy

Selecting the correct approach depends on the context. For display purposes, use f-string formatting because it is concise and does not alter the original value. For general numeric rounding where a small bias is acceptable, round() is fine. For financial calculations, legal reporting, or any situation where the exact decimal value matters, use Decimal with an explicit rounding mode. If you are rounding to an integer and need to control the direction, use math.floor() or math.ceil().

The key is to understand that rounding Python floats is not a single operation with one universal behavior. The binary representation of floats makes exact decimal rounding impossible without a decimal type. By choosing the right tool for each situation, you avoid the most common rounding errors and keep your code predictable and maintainable.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.