denysdovhan / wtfjs

🤪 A list of funny and tricky JavaScript examples
http://bit.ly/wtfjavascript
Do What The F*ck You Want To Public License
34.97k stars 2.55k forks source link

Correct explanation for the toFixed oddities. #80

Closed mcmonkey4eva closed 6 years ago

mcmonkey4eva commented 6 years ago

The original explanation present was: View the Firefox source, 'toFixed' method is to convert the value of the conversion, not the standard implementation.

... That does not make sense... convert the value of the conversion? What?

It's just Firefox and Chrome obeying IEEE-754 Floating Point standards as closely as possible, and Internet Explorer doing whatever IE does all day (nobody really knows).

In fact, you can see that C++ will do the same thing: double x = 0.7875; cout << setprecision(3) << x << endl; yields 0.787, demonstrated on: https://ideone.com/TJzyhj

denysdovhan commented 6 years ago

Thanks for clarifying!