In comp.lang.javascript message <
[email protected]
september.org>, Sun, 2 Aug 2009 23:27:06, Garrett Smith
You have not answered the explicit question - is it safe to call
toFixed? I expressed no interest in whether it is safe to believe the
result of the call. I was only interested in whether any reasonably
current browsers would in effect say "toFixed? never heard of it".
I'm not sure that toFixed is all that useful, due to rounding issues. I
mean, it is not something that could be used for money, right?
Money is not the only application in the world.
Money calculations should be done in integer arithmetic; cents for
ordinary work, M€ for eurocrats. <FAQENTRY> The FAQ should be modified
to say that.
Jibbering seems down.
A mirror is needed.
1.1255.toFixed(3);
IE7, some Safari versions:
1.126
Others:
1.125
1.1255 cannot be represented exactly in a Double. There is no certainty
that +"1.1255" gives the same IEEE Double in all browsers, whatever the
Standard may say. And there is likewise no certainty that each of those
IEEE Doubles will, even in a carefully-written browser, will give the
same result on any conversion to string.
On the same lines : It was settled that one euro would be equal to
exactly 0.787564 punts. That did not mean that the Bank could take Tim
O'Shea & Co's balance of, say, IEP 1234567.89, as an IEEE Double, divide
it by 0.787564, and rewrite it as EUR 1567577.86; the required
arithmetic was expressed exactly, including rounding, in decimal. I
suspect they'd have ended up with EUR 1567580, but that is a guess.
Is it? I see 0.95.toFixed(0)
IE7: 1
I wrote 0.50 to 0.95, carefully without saying whether the ends of the
range were to be included. In fact, as you must know, 0.95 cannot be
represented exactly by an IEEE Double.
JScript :
0.94999999999999990.toFixed(0) -> 0
0.94999999999999991.toFixed(0) -> 1
JScript only, or implementations in general?
I was not then saying. I now say that is browser-dependent; there seems
to be a misinterpretation of the standard in at least one case. Work is
in hand. ... For X.toFixed(0), where X is a half-integer, Opera 9.64
does Banker's Rounding, contrary to ISO/IEC 16262 15.7.4.5 #10.
Reported. Other browsers correctly round away from zero.
and is more updated.
Tests comparing the numerical values of the strings given by
toExponential & toPrecision with the results of my own conversions in 5
browsers, for random inputs have shown no problems. Testing the same
values with toFixed in 4 browsers showed no problems. There was one
case where the input was very close to half-way between the two adjacent
possible output values obtained.
Those tests need to be run for longer.