polars.Expr.truncdiv#

Expr.truncdiv(other: Any) → Expr[source]#

Divide, rounding the quotient toward zero.

Unlike floordiv(), which rounds down, this rounds a negative quotient up, as integer division does in C and Rust. The result has the same data type as floordiv’s.

engine:In MemoryStreamingDistributed
Parameters:
other

Numeric literal or expression value.

See also

floordiv
truediv

Examples

>>> df = pl.DataFrame({"x": [-5, -4, 4, 5]})
>>> df.with_columns(
...     pl.col("x").floordiv(2).alias("x//2"),
...     pl.col("x").truncdiv(2).alias("truncdiv(x, 2)"),
... )
shape: (4, 3)
┌─────┬──────┬────────────────┐
│ x   ┆ x//2 ┆ truncdiv(x, 2) │
│ --- ┆ ---  ┆ ---            │
│ i64 ┆ i64  ┆ i64            │
╞═════╪══════╪════════════════╡
│ -5  ┆ -3   ┆ -2             │
│ -4  ┆ -2   ┆ -2             │
│ 4   ┆ 2    ┆ 2              │
│ 5   ┆ 2    ┆ 2              │
└─────┴──────┴────────────────┘