I've come across a curious issue in one of my unit tests where I'm getting unexpected rounding results in JavaScript:
(2.005).toFixed(2)
// produces "2.00"
(2.00501).toFixed(2)
// produces "2.01"
Initially I suspected this was a Webkit only issue but it repros in Gecko which implies to me that it is an expected side effect of either ECMA-262 or IEEE-754. I'm assuming the binary representation of 2.005 is ever so slightly less? Or does ECMA-262 specify a round-to-even methodology for toFixed
?
Anyone care to shed some insight as to what is happening under the hood just to give me peace of mind?
Update: thanks for the comments.
I should add, one of the things that made me a little nervous was the comments found in a quick search in Webkit dtoa.cpp
which seemed to imply that there were multiple paths to rounding and the devs weren't really sure how it worked, including a related FIXME
:
https://trac.webkit.org/browser/trunk/Source/WTF/wtf/dtoa.cpp#L1110
Also, not that it means much but IE9 rounds it as I expected, implying that it either isn't part of ECMA-262 or they have a bug.