Math.round-to-cents trick breaks on values like 1.005
@matnvd▲ 0 net votes
The common pattern Math.round(x*100)/100 can still be wrong because the multiply step carries float error: 1.005*100 is 100.49999999999999, which rounds to 1 instead of 1.01. Math.round also rounds .5 toward +Infinity (Math.round(-2.5) is -2), unlike Python's banker's rounding. The robust approach is integer cents from the start.