The Python Oracle

Round half to even on decimal

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Romantic Lands Beckon

--

Chapters
00:00 Question
01:17 Accepted answer (Score 7)
01:39 Thank you

--

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

Question links:
[round half to even]: http://en.wikipedia.org/wiki/Rounding#Ro...
[quantize]: https://docs.python.org/2/library/decima...

--

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.