A robot cell’s inspection program may export a measured gap to three decimal places while its review worksheet accepts quarter-millimeter increments. Those are two different parts of the reporting contract. The first describes the digits received from the instrument. The second lists the values the worksheet can represent. A report can preserve two decimal places and still contain a value that is off the required grid.

Consider a reading of 12.37 mm. It falls between the reporting points 12.25 mm and 12.50 mm. It is 0.12 mm from the first and 0.13 mm from the second, so the nearest reporting point is 12.25 mm. Passing Decimal("0.25") as the argument to Decimal.quantize produces 12.37. The argument supplies an exponent of minus two, and the input already has that exponent. The coefficient 25 does not define the distance between permitted outputs.

Work in counts of the reporting increment

Divide the measured value by the increment to express it as a number of steps. Round that count to an integer under the reporting policy, then multiply by the increment to return to millimeters. In the example, 12.37 mm corresponds to 49.48 quarter-millimeter steps. Rounding to 49 steps gives 12.25 mm.

The following fixture uses exact decimal strings and a deliberately chosen tie rule for signed deviations. ROUND_HALF_UP sends a halfway count away from zero. If an instrument contract requires a different rule, use that rule explicitly and write a fixture for both sides of zero.

from decimal import Decimal, ROUND_HALF_UP, localcontext

step = Decimal("0.25")

cases = {

"12.370": "12.25",

"12.375": "12.50",

"12.380": "12.50",

"-0.125": "-0.25",

"12.500": "12.50",

}

with localcontext() as context:

context.prec = 28

for raw, expected in cases.items():

count = (Decimal(raw) / step).quantize(

Decimal("1"), rounding=ROUND_HALF_UP

)

reported = count * step

assert str(reported) == expected

assert Decimal("12.37").quantize(step) == Decimal("12.37")

The three positive readings around 12.375 exercise the point where the report changes from 12.25 to 12.50. The signed case checks that the same tie rule applies to a negative deviation. An on-grid reading checks that a valid result stays where it belongs. The string assertions also check the worksheet representation, including the trailing zero in 12.50. Numeric equality alone would accept 12.5 in its place.

Keep the measurement and the report field

Store the incoming decimal value in its original unit alongside the rounded worksheet value. Also store the increment and the rounding rule in the export configuration. This lets a reviewer reproduce the result after the equipment or report format changes. For an acceptance decision, use the value and rounding procedure named by the applicable inspection specification. A display rule by itself does not establish how a tolerance should be evaluated.

This fixture covers a finite range of decimal readings and a step of 0.25, which gives an exact decimal quotient for these inputs. When a different step can produce a repeating quotient, review the arithmetic precision and test readings close to the half-step boundaries over the supported measurement range. A context that rounds the quotient before the final step can affect which side of a boundary is chosen.

The Python documentation defines the exponent and rounding behavior of quantize. The General Decimal Arithmetic specification describes the operation in terms of the two operands, including cases that preserve the first value while changing its precision. The reporting grid is an application rule applied before converting the step count back to the instrument unit.

Sources

Python decimal documentation: https://docs.python.org/3/library/decimal.html#decimal.Decimal.quantize

General Decimal Arithmetic, quantize: https://speleotrove.com/decimal/daops.html#refquant