Closed Bug 463163 Opened 16 years ago Closed 16 years ago

Number.toFixed method returns a wrong rounded value

Categories

(Core :: JavaScript Engine, defect)

1.9.0 Branch
x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 186563

People

(Reporter: lhwci, Unassigned)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3

when I use toFixed method fro rounding a decimal with a fixed decimal digits and I just got a unexpected value.

Reproducible: Always

Steps to Reproduce:
1. "1.015".toFixed(2),"1.025".toFixed(2),"1.035".toFixed(2),"1.045".toFixed(2),etc..
2. I got 1.01 all the time, but I think it should be 1.02 instead
3.
Actual Results:  
as described above
Assignee: nobody → general
Component: General → JavaScript Engine
Product: Firefox → Core
QA Contact: general → general
Version: unspecified → 1.9.0 Branch
(In reply to comment #0)
> Steps to Reproduce:
> 1. "1.015".toFixed(2),"1.025".toFixed(2),"1.035".toFixed(2),"1.045".toFixed(2),etc..

Unable to re-produce, because of following error.
> Error: "1.015".toFixed is not a function
Please note that toFixed() is method of number object.

Test result with number_object.toFixed() (Fx 3.0.3 / Seamonkey on MS Win-XP SP3)
> javascript:var x=1.025;               var y=x.toFixed(2);alert(y); => 1.02
> javascript:var x=1.02500000000000001; var y=x.toFixed(2);alert(y); => 1.02
> javascript:var x=1.0250000000000001;  var y=x.toFixed(2);alert(y); => 1.03
Phenomenon was observed. 
It looks to be a result of cut-off error by use of floating point register.
yeah, so sorry, it's my mistake of making a wrong reproducing step.
it should be a number instead of String.
but the result is exactly what I reported
var num=1.015;
var x=num.toFixed(2);alert(x); ==> 1.01
etc..
cheers
This is a dupe of 5856, I think.
Status: UNCONFIRMED → RESOLVED
Closed: 16 years ago
Resolution: --- → DUPLICATE
This is not a dup of bug 5856.

/be
The JS token 1.015 converts to the IEEE double 1.0149999999999999. No operation required, contrast to bug 5856. Seems to be worth keeping this bug around as the more specific dup target (it has been used that way before).

/be
(In reply to comment #6)
> The JS token 1.015 converts to the IEEE double 1.0149999999999999. No operation
> required, contrast to bug 5856. Seems to be worth keeping this bug around as
> the more specific dup target (it has been used that way before).

By "this bug" I meant bug 186563, of course.

/be
sorry guys, I couldnot agree with you that this bug is a dup of bug 5856 even bug 186563, I already read those two bugs and didn't find they represent the same issue.
Did you read bug 186563 comment 13, bug 186563 comment 17, and bug 186563 comment 18 carefully?

If so, at what step in which comment do you believe this bug's testcase differ from that bug's?

/be
You need to log in before you can comment on or make changes to this bug.