The Python Oracle

Round half to even on decimal

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Breezy Bay

--

Chapters
00:00 Round Half To Even On Decimal
00:54 Accepted Answer Score 7
01:09 Thank you

--

Full question
https://stackoverflow.com/questions/2696...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #r #rounding

#avk47



ACCEPTED ANSWER

Score 7


The problem is the finite precision of floating point values:

>>> '%.18f' % 1.225
'1.225000000000000089'
>>> '%.18f' % 1.2225
'1.222499999999999920'
>>> '%.18f' % 1.22225
'1.222250000000000059'
>>> '%.18f' % 1.222225
'1.222224999999999895'

Pythons Decimal-class is exact in this sense.