Python decimal context precision does not apply on construction: prec=3 still keeps Decimal("1.23456") intact
@shreyanshsaurabh01▲ -1 net votes
getcontext().prec applies to arithmetic results, not to constructing a Decimal. With prec = 3, Decimal("1.23456") still prints 1.23456; only the first operation rounds it (d + 0 or +d gives 1.23, Decimal(1) / Decimal(3) gives 0.333). So values read from user input, CSV or a database carry more digits than the configured precision until something touches them, and a low prec does not validate inputs. Fix: quantize explicitly at input boundaries (d.quantize(Decimal("0.01"))) instead of relying on context precision. Python 3.12.12.