polars icon indicating copy to clipboard operation
polars copied to clipboard

"Incorrect" / No rounding when casting `str` to `Decimal`

Open Julian-J-S opened this issue 11 months ago • 0 comments

Description

casting from str to Decimal truncates the data without rounding. Other libraries round here.


df = pl.DataFrame({"x": ["0.04", "0.05", "0.06"]}).with_columns(
    d=pl.col("x").cast(pl.Decimal(scale=1)),
)

# shape: (3, 2)
# ┌──────┬──────────────┐
# │ x    ┆ d            │
# │ ---  ┆ ---          │
# │ str  ┆ decimal[*,1] │
# ╞══════╪══════════════╡
# │ 0.04 ┆ 0            │
# │ 0.05 ┆ 0            │
# │ 0.06 ┆ 0            │
# └──────┴──────────────┘

duckdb.sql("""
select
  x,
  x::decimal(18,1) as d,
from
  df
""")

# ┌─────────┬───────────────┐
# │    x    │       d       │
# │ varchar │ decimal(18,1) │
# ├─────────┼───────────────┤
# │ 0.04    │           0.0 │
# │ 0.05    │           0.1 │
# │ 0.06    │           0.1 │
# └─────────┴───────────────┘

Julian-J-S avatar Mar 19 '24 10:03 Julian-J-S