0

I am implementing excel roundup function using python 3.6 and math.ceil returns the weird results.

math.ceil(0.56 * 100) returns 57 while math.ceil(56) and math.ceil(56.0) return 56.

What should I do to get the correct result for math.ceil(a * b)?

discover
  • 411
  • 1
  • 6
  • 16
  • 1
    you should check [is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Tortar Dec 06 '21 at 12:48
  • Thanks @Tortar. I checked it and want to get correct result in python. – discover Dec 06 '21 at 12:58
  • as Tortar pointed out, `0.56 * 100 != 56`, it expands to `56.00000000000001` which is your issue. So either only take the first x decimal points, or try something else – Mahrkeenerh Dec 06 '21 at 12:59

1 Answers1

0

I think math.ceil(round(0.56 * 100, 2)) solves the issue.

Any better method would be appreciated.

discover
  • 411
  • 1
  • 6
  • 16