0

When I am returning my function, the answers are inconsistent. I am trying to round to .05. example: 26.93 -> 26.95 Here is what happens.. What did I do wrong? Thanks

 >>> cashback = (0.05 * round(payment/0.05)) 

 >>> cad_cashier(23,26.93)
 'The change value is: 3.950000000000003'

 >>> cashback = (0.05 * round((payment) / 0.05))
 >>> cad_cashier(23,26.52)
 'The change value is: 3.5'

Why does one value work but the other value adds 0003?

Edward Minnix
  • 2,889
  • 1
  • 13
  • 26
ghDev
  • 170
  • 1
  • 12
  • Related: [Limiting floats to two decimal points](https://stackoverflow.com/q/455612/364696) – ShadowRanger Sep 29 '17 at 04:41
  • The conversion from decimal floating point to binary floating point involves some approximation in many cases. A number ending in .5 works perfectly in binary. – Mark Ransom Sep 29 '17 at 04:47
  • For Python you can use the [`Decimal` module](https://docs.python.org/3.6/library/decimal.html) to eliminate rounding errors caused by binary arithmetic. – Mark Ransom Sep 29 '17 at 04:53
  • 1
    But python 3 does a good job at rounding floats to n decimals, and binary shifts *2 and /2 won't arm, so `round(2*x,1)/2` could possibly work, even if counting money with float is a dangerous sport with many pitfalls – aka.nice Sep 29 '17 at 07:32
  • Thank you. So I will look up decimal module – ghDev Sep 29 '17 at 14:40

0 Answers0