Closed Bug 397880 Opened 19 years ago Closed 19 years ago

Incorrect rounding by toFixed() for some values when last decimal digit is 5

Categories

(Core :: JavaScript Engine, defect)

PowerPC
macOS
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 226993

People

(Reporter: bugzilla, Unassigned)

References

Details

User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-GB; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7 Build Identifier: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-GB; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7 This example cropped up when using toFixed(2) and is described here but also occurs for other precisions (see additional information). When rounding some number that are expressed to exactly 3 decimal places with the third decimal place being a 5 then the number is round down rather than up. E.g. if a=1.055 then a.toFixed(2) should return 1.06, but instead it returns 1.05. There doesn't seem to be a clear pattern as to which values of n.nn5 are incorrectly rounded down. Reproducible: Always Steps to Reproduce: Simple script to output values of n.nn5 which don't round up (eval used so that "var a" doesn't start with any rounding errors that would happen if you simply looped by adding 0.01 each time): var m=0; var n=2; for (var i=m;i<=n;i++){ for (var j=0;j<100;j++){ x(i,j); } } function x(i,j) { var z = j<10 ? "0" : ""; var a = eval(i+"."+z+j+"5"); var b = a.toFixed(2); if (b<a) {document.write(a+" -> "+b+"<br/>");} } Actual Results: 0.015 -> 0.01 0.045 -> 0.04 0.075 -> 0.07 0.105 -> 0.10 0.145 -> 0.14 0.155 -> 0.15 0.175 -> 0.17 0.185 -> 0.18 0.205 -> 0.20 0.215 -> 0.21 0.235 -> 0.23 0.245 -> 0.24 0.285 -> 0.28 0.295 -> 0.29 0.305 -> 0.30 0.345 -> 0.34 0.355 -> 0.35 0.365 -> 0.36 0.415 -> 0.41 0.425 -> 0.42 0.435 -> 0.43 0.475 -> 0.47 0.485 -> 0.48 0.495 -> 0.49 0.565 -> 0.56 0.575 -> 0.57 0.585 -> 0.58 0.595 -> 0.59 0.605 -> 0.60 0.615 -> 0.61 0.695 -> 0.69 0.705 -> 0.70 0.715 -> 0.71 0.725 -> 0.72 0.735 -> 0.73 0.745 -> 0.74 0.815 -> 0.81 0.825 -> 0.82 0.835 -> 0.83 0.845 -> 0.84 0.855 -> 0.85 0.865 -> 0.86 0.945 -> 0.94 0.955 -> 0.95 0.965 -> 0.96 0.975 -> 0.97 0.985 -> 0.98 0.995 -> 0.99 1.005 -> 1.00 1.015 -> 1.01 1.025 -> 1.02 1.035 -> 1.03 1.045 -> 1.04 1.055 -> 1.05 1.065 -> 1.06 1.075 -> 1.07 1.085 -> 1.08 1.095 -> 1.09 1.105 -> 1.10 1.115 -> 1.11 1.255 -> 1.25 1.265 -> 1.26 1.275 -> 1.27 1.285 -> 1.28 1.295 -> 1.29 1.305 -> 1.30 1.315 -> 1.31 1.325 -> 1.32 1.335 -> 1.33 1.345 -> 1.34 1.355 -> 1.35 1.365 -> 1.36 1.505 -> 1.50 1.515 -> 1.51 1.525 -> 1.52 1.535 -> 1.53 1.545 -> 1.54 1.555 -> 1.55 1.565 -> 1.56 1.575 -> 1.57 1.585 -> 1.58 1.595 -> 1.59 1.605 -> 1.60 1.615 -> 1.61 1.755 -> 1.75 1.765 -> 1.76 1.775 -> 1.77 1.785 -> 1.78 1.795 -> 1.79 1.805 -> 1.80 1.815 -> 1.81 1.825 -> 1.82 1.835 -> 1.83 1.845 -> 1.84 1.855 -> 1.85 1.865 -> 1.86 2.005 -> 2.00 2.025 -> 2.02 2.045 -> 2.04 2.065 -> 2.06 2.085 -> 2.08 2.105 -> 2.10 2.135 -> 2.13 2.155 -> 2.15 2.175 -> 2.17 2.195 -> 2.19 2.215 -> 2.21 2.235 -> 2.23 2.255 -> 2.25 2.275 -> 2.27 2.295 -> 2.29 2.315 -> 2.31 2.335 -> 2.33 2.355 -> 2.35 2.385 -> 2.38 2.405 -> 2.40 2.425 -> 2.42 2.445 -> 2.44 2.465 -> 2.46 2.485 -> 2.48 2.505 -> 2.50 2.525 -> 2.52 2.545 -> 2.54 2.565 -> 2.56 2.585 -> 2.58 2.605 -> 2.60 2.635 -> 2.63 2.655 -> 2.65 2.675 -> 2.67 2.695 -> 2.69 2.715 -> 2.71 2.735 -> 2.73 2.755 -> 2.75 2.775 -> 2.77 2.795 -> 2.79 2.815 -> 2.81 2.835 -> 2.83 2.855 -> 2.85 2.885 -> 2.88 2.905 -> 2.90 2.925 -> 2.92 2.945 -> 2.94 2.965 -> 2.96 2.985 -> 2.98 Expected Results: Should be no output, i.e. calling toFixed(2) on each n.nn5 should round the value up. Between each integer range the decimal values that fail vary, I haven't looked deeply to see if there's any pattern. Some values are okay for a few integer values but not for any others, e.g. N.055 is okay for N=0 and N=2 but for no other values of N I've found. Other precisions are affected, e.g. if a=N.05 then toFixed(1) only returns N.1 when N is 0 or 1. Similarly if a=N.0055 then toFixed(3) only returns N.006 when N is 1,2 or 3. A simple workaround is to artifically add a tiny amount relative to the input value (e.g. 0.0000001) to get it to round up. Originally found on 2.0.0.7 on Mac OS X but also reproduced on 1.5.0.5 on Windows XP. Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-GB; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7 Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.5) Gecko/20060719 Firefox/1.5.0.5
Assignee: nobody → general
Component: General → JavaScript Engine
Product: Firefox → Core
QA Contact: general → general
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → DUPLICATE
A plea to someone regarding the bug search: I did search for toFixed to see if this bug had been reported before and it only came up with one unrelated match. This bug was a duplicate of 226993 which also had another duplicate (381411) and in turn was itself a duplicate of 186563. All of them had toFixed in the bug title but none of them (including this one) show up in the search! I and they spent a fair bit of time providing detailed bug reports, I wouldn't want anyone else to waste their time on what I'm sure someone will re-discover again. Anyone know why these bugs don't show up? (BTW, although the output may follow IEEE 754 this is such an expected result for the user that it must cause more problems than it solves. But anyway, crude hack in the code to get the expected output it is then...)
"But anyway, crude hack in the code to get the expected output it is then..." (...that is, in my client-side code)
Duplicate of this bug: 1845050
You need to log in before you can comment on or make changes to this bug.