PYTHON_ROUND

Round a numeric value using Python / IEEE 754 semantics (round half to even).

Category: mathReturns: DOUBLEDialect: Standard

Syntax

PYTHON_ROUND(expr, d)

Description

## Overview Returns the input rounded to the specified number of decimal places using round-half-to-even semantics, matching Python's built-in round() function on floats. The behavior is identical to BROUND except that the scale argument is mandatory, eliminating ambiguity when callers port code from Python. Use PYTHON_ROUND when you need bit-for-bit reproducibility with a Python data pipeline that consumes the SQL output. For general banker's-rounding workloads where the default scale of 0 is acceptable, BROUND is usually more convenient. ## Behavior - Both arguments are required. - Returns NULL if either argument is NULL. - Uses round-half-to-even: 2.5 -> 2, 3.5 -> 4, 2.45 -> 2.4, 2.35 -> 2.4. - Negative scale rounds to the left of the decimal point. - Matches Python's round() behavior on DOUBLE inputs exactly, including the surprises caused by binary floating-point representation. ## Numeric precision - For DECIMAL inputs, rounding is exact. - For DOUBLE inputs, the binary representation of values like 2.675 can cause round-half-to-even to select the 'wrong' neighbor. This is intentional: the goal of PYTHON_ROUND is to match Python, which has the same behavior. - Pair with DECIMAL storage for exact decimal rounding. ## Compatibility - Matches Python's round() function on floats exactly. - Implements the IEEE 754 default rounding mode (round half to even).

Parameters

NameTypeDescription
exprSpecifies the numeric value to round. Accepts any numeric type.
dSpecifies the number of decimal places. Required (unlike ROUND and BROUND, there is no default). Negative values round to the left of the decimal point.

Examples

-- Halfway rounds to even: 2.5 rounds to 2
SELECT PYTHON_ROUND(2.5, 0);
-- Result: 2.0
-- Halfway rounds to even: 3.5 rounds to 4
SELECT PYTHON_ROUND(3.5, 0);
-- Result: 4.0
-- Round to 2 decimal places (note floating-point representation surprises)
SELECT PYTHON_ROUND(2.675, 2);
-- Result: 2.67
-- Negative decimal places
SELECT PYTHON_ROUND(125, -1);
-- Result: 120.0
-- NULL propagation
SELECT PYTHON_ROUND(NULL, 2);
-- Result: NULL
-- Column use: match a downstream Python pipeline's rounding exactly
SELECT event_id, PYTHON_ROUND(metric_value, 4) AS rounded_metric
FROM analytics.ml.evaluation_metrics;

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →