What Test Checks Rounding in a Public Metric?
From the earlier reference collection. Read the current software blog.
Test documented display precision at values on both sides of rounding boundaries, including negative values if the metric allows them.
Start from the published rule
Identify the metric’s stated precision and rounding convention before writing expected values. A display rule might round to a fixed number of decimal places, but the choice of half-up, half-even, truncation, or another convention can change boundary results. The test should reflect the public contract rather than the behavior of a particular formatting library by accident.
Keep calculation precision separate from display precision. A metric may retain a more precise internal value and round only when rendered or exported. Test the layer that owns the rule, then add an integration assertion that the public representation follows it.
Choose values around a boundary
For a rule that displays one decimal place, test values just below, exactly at, and just above a halfway point, such as 1.24, 1.25, and 1.26. Set the expected strings from the documented convention. A value far from a boundary, such as 1.23, cannot distinguish several rounding methods.
Include a value that carries into the next whole number, such as 9.96 under one-decimal rounding, and a negative value if negative readings are valid. Negative halfway behavior is a common source of disagreement between “toward zero” and “away from zero” interpretations, so state the intended result clearly.
Check units and serialization
Verify that rounding does not accidentally alter units or omit meaningful trailing zeros when the display contract requires fixed precision. A value shown as 2.0 can communicate a different precision than 2, even though they are numerically equal. Test the formatted representation as well as the underlying rounded number where both are exposed.
For machine-readable exports, follow the export contract: it may preserve full precision while the dashboard rounds for readability. Do not apply a display rule to stored data unless that is explicitly required.
Make the test resistant to floating-point surprises
Use decimal arithmetic or carefully chosen representable fixtures when the language’s binary floating-point behavior could affect a boundary assertion. Otherwise, the test may fail because the input is stored slightly above or below the intended decimal, obscuring the rounding rule.
Name cases around the boundary and convention. The result should make it easy to see whether the bug lies in calculation, formatting, units, or an unclear product rule. A few deliberate values provide a stronger check than many arbitrary decimals.