0

I want to round numbers using python, but round(1.45, 1) is returning 1.4 whereas using normal rounding rules the answer should be 1.5.

How can I change this behaviour?

Mark Dickinson
  • 29,088
  • 9
  • 83
  • 120
guroosh
  • 642
  • 6
  • 18
  • 6
    Possible duplicate of [How to properly round up half float numbers in Python?](https://stackoverflow.com/questions/33019698/how-to-properly-round-up-half-float-numbers-in-python) – Glazbee Apr 15 '19 at 12:11
  • That's not a great duplicate, since that's about halfway cases (ties), that round differently depending on rounding mode (round-ties-to-even, round-ties-to-away, ...). Thanks to the what-you-see-is-not-what-you-get nature of binary floating-point, `1.45` is _not_ a halfway case. The actual value stored is (very likely to be) `1.4499999999999999555910790149937383830547332763671875`, which _should_ round to `1.4` under either round-ties-to-even _or_ round-ties-to-away. By the way, "normal rounding rules" is rather loaded language: who decides what's "normal"? – Mark Dickinson Apr 19 '19 at 15:16
  • How about the "standard rounding rules", used in google sheets `ROUND` function [link](https://support.google.com/docs/answer/3093440?hl=en) – guroosh Apr 20 '19 at 05:10

0 Answers0