Closed
Bug 226993
Opened 21 years ago
Closed 17 years ago
Javascript toFixed rounds in unexpected manner
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
RESOLVED
DUPLICATE
of bug 186563
People
(Reporter: michel, Unassigned)
References
Details
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.5) Gecko/20031007
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.5) Gecko/20031007
(0.045).toFixed(2)
I would expect .05 like IE gives and not .04
Reproducible: Always
Steps to Reproduce:
1. type javascript:alert((0.045).toFixed(2)) into location bar
2. hit enter
3. marvel at the result
Actual Results:
0.04
Expected Results:
0.05
Comment 1•21 years ago
|
||
Confirmed. 1.6a/w2k.
We definitely should be rounding up, by my reading of ECMA-262 (15.7.4.5), and
indeed we do appear to, for some cases:
(0.5).ToFixed(0) == "1" (0.05).ToFixed(1) == "0.1"
(1.5).ToFixed(0) == "2" (0.15).ToFixed(1) == "0.1"
(2.5).ToFixed(0) == "3" (0.25).ToFixed(1) == "0.3"
(3.5).ToFixed(0) == "4" (0.35).ToFixed(1) == "0.3"
(4.5).ToFixed(0) == "5" (0.45).ToFixed(1) == "0.5"
(5.5).ToFixed(0) == "6" (0.55).ToFixed(1) == "0.6"
(6.5).ToFixed(0) == "7" (0.65).ToFixed(1) == "0.7"
(7.5).ToFixed(0) == "8" (0.75).ToFixed(1) == "0.8"
Status: UNCONFIRMED → NEW
Ever confirmed: true
Comment 2•21 years ago
|
||
(this may be caused by the fact that we can't represent 0.5 exactly as a
floating-point number, of course, which would make this a duplicate of bug
186563).
Comment 3•21 years ago
|
||
And in fact, it is. This bug (*exactly* this report, no less) is covered at
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/number
.html#1207032
Extract:
"The IEEE 754 standard dictates that 0.045 be approximated to
0.04499999999999999833466546306226518936455249786376953125, which is precisely
representable as a binary fraction. This approximation is closer to 0.04 than
to 0.05, so 0.045.toFixed(2) returns "0.04"."
INVALID.
Status: NEW → RESOLVED
Closed: 21 years ago
Resolution: --- → INVALID
Comment 4•21 years ago
|
||
Marking Verified -
Michel, thank you for this report; this issue often comes up.
See bug 186563 for more discussion.
Malcolm, thank you for your research on this!
Status: RESOLVED → VERIFIED
Reporter | ||
Comment 5•21 years ago
|
||
Okeee, thanks. I did search for toFixed in bugzilla javascript and did no find it.
For information IE6 does round up since they are probably more pragmatic about
it and asks themselves what does the user expect rather than how is the number
represented in the IEEE 754 - which is why more nerds use mozilla (as do I) ;)
Updated•17 years ago
|
Severity: normal → critical
Status: VERIFIED → RESOLVED
Closed: 21 years ago → 17 years ago
Resolution: INVALID → DUPLICATE
You need to log in
before you can comment on or make changes to this bug.
Description
•