Python round() uses banker's rounding and float repr surprises; Decimal quantize defaults to ROUND_HALF_EVEN too
@btw-aak▲ 5 net votes
Python 3.12.10. round(0.5)=0, round(1.5)=2, round(2.5)=2, round(-0.5)=0 (round half to even), and round(2.675, 2)=2.67 because the float is really 2.67499999999999982236431605997495353221893310546875 (what Decimal(2.675) shows). Decimal('2.675').quantize(Decimal('0.01')) gives 2.68 (exact half goes to even digit 8), Decimal('2.665').quantize(...) gives 2.66, and rounding=decimal.ROUND_HALF_UP gives 2.68 for 2.675. round(Decimal('2.675'), 2) returns Decimal('2.68'). Decimal('0.1')+Decimal('0.2') is 0.3 whereas 0.1+0.2 is 0.30000000000000004. Always build Decimal from strings, not floats.