Data and reporting

Python Decimal Formatting and Rounding

Display two decimal places with an f-string, apply an explicit tie rule with Decimal.quantize, and decide whether to round before or after summing.

Use f"{value:.2f}" to display a number with two decimal places. If the calculation itself needs a decimal rounding rule, create a Decimal from the input string and apply that rule with quantize(). The display string and the value you use in later arithmetic can have different precision.

Format a display value without changing the input

value = 12.345
display = f"{value:.2f}"
assert isinstance(display, str)
assert value == 12.345

Formatting returns a string. It does not alter the original variable or guarantee a decimal arithmetic policy across all values. Floats approximate many decimal fractions. A value entered with a decimal tie can lie just above or below the apparent tie after conversion to binary floating point. For a chart label or an estimated measurement, this is often fine. If the report must obey a specified tie rule, preserve decimal input before applying it.

Separators and labels are another display choice. f"{1234.5:,.2f}" produces 1,234.50. A human can read that easily in contexts using comma grouping and a dot decimal separator. A machine-readable file typically needs a stable convention. Decide whether to export a plain decimal string, integer minor units, or another form expected by its consumer.

Round a Decimal with quantize

from decimal import Decimal, ROUND_HALF_UP

quantity = Decimal("12.345")
rounded = quantity.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
assert rounded == Decimal("12.35")
assert f"{rounded:.2f}" == "12.35"

The input is a string so the intended fraction is available exactly. Decimal(12.345) would begin from the existing float approximation. quantize() applies the exponent from its second operand, with the specified tie behavior in this example. Different reporting rules may call for half even, half up, truncation, or a different precision. Confirm that rule rather than deriving it from a few sample outputs.

If you parse user text, validate the input convention first. A string such as 1,234 means different things to different audiences. Do not blindly delete commas or spaces. Also decide whether scientific notation or nonfinite values are permitted. A well-formed Decimal object can still violate the input contract.

Round each amount or round the total

Consider three amounts of 1.005 in the same units. Under half-up rounding to two places, rounding each amount gives 1.01 three times, producing 3.03. Adding the exact amounts first gives 3.015, which rounds to 3.02. The discrepancy comes from the placement of rounding. A total in a report should use the rule expected by its consumers.

Integer minor units are another option when every amount is a whole number of fixed minor units. The input contract needs to say which scale applies. Multiplication or division may still produce fractions that require rounding. Keep amounts in comparable units and avoid inferring scale from the number of decimal places the user entered. Different currencies and measurements can use different scales.

Test boundaries that expose the policy

Use values just below a tie, exactly at the tie and just above it. Include negative values if they are permitted. Verify both the underlying rounded numeric value and its display string, including trailing zeros if they matter. If multiple systems exchange the value, verify that the receiving system does not parse it through a different precision policy.

State the units and rounding point in your report. A line such as “Totals use unrounded measurements and display two decimals” can prevent a reviewer from trying to reproduce the total by adding already displayed values. When an exact audit trail matters, store the inputs and rounding rule alongside the output.

Sources and further reading

Related reading

Browse Data and reporting · Explore project tutorials

Search PythonProjects.org