Opened 11 years ago

Closed 10 years ago

#7930 closed defect (wontfix)

dojo.number.round(162.295, 2) returns 162.29

Reported by: kschneid Owned by: Adam Peller
Priority: high Milestone: tbd
Component: General Version: 1.1.0
Keywords: Cc:
Blocked By: Blocking:

Description

I'm doing some currency calculations and getting bitten by JavaScript's handling of floating point numbers. I was hoping that dojo.number.round would address the issues of typical solutions, like:

Math.round(162.295 * 100) / 100

In this case, (162.295 * 100) = 16229.499999999998, which causes the call to Math.round to return 16229 (it should return 16230). Although dojo.number.round is a more general-purpose solution, it still relies on:

Math.round(value * factor)/factor

which I believe is causing the problem.

Change History (9)

comment:1 Changed 11 years ago by Adam Peller

Owner: changed from anonymous to Adam Peller
Status: newassigned

comment:2 Changed 11 years ago by Adam Peller

Milestone: tbd1.3
Resolution: fixed
Status: assignedclosed

Fixed in [15634)

comment:3 Changed 11 years ago by Adam Peller

Fixed in [15634] and [15941]

comment:4 Changed 11 years ago by Adam Peller

the new implementation adds a small delta which tries to compensate for floating point artifacts, at least if they were not arithmetically augmented prior to the call. Within the method, we now use Number.toFixed() which does not require the extra arithmetic to shift decimal places and should be *much* faster. There is still a multiply/divide by factor, which is usually 1 or 2, depending on the parameters, and therefore would not cause any additional damage unless irregular values were provided for 'increment'. Hope this helps.

comment:5 Changed 11 years ago by Adam Peller

(In [16814]) Support 14 sig figs. Fixes #8699, Refs #7930 !strict

comment:6 Changed 11 years ago by Adam Peller

see [16820]. Moved this rounding trick out of dojo.number.round to dojox.math.round as an experimental routine, both for backwards compat and because of the range limitation for accuracy, and added documentation to warn of the possibility of floating point rounding error.

comment:7 Changed 10 years ago by Adam Peller

Milestone: 1.3tbd
Resolution: fixed
Status: closedreopened

Hmm... this may be a FF bug after all, at least now that we're using toFixed in dojo.number.round

https://bugzilla.mozilla.org/show_bug.cgi?id=484293

comment:8 Changed 10 years ago by bill

[17053] comments out the tests until this bug is fixed.

comment:9 Changed 10 years ago by Adam Peller

Resolution: wontfix
Status: reopenedclosed

I think the best we can do for now is offer the fudge factor solution at dojox.math.round, but leave dojo.number alone to avoid issues with compatibility and accuracy, and recognize that this is an inherent problem in the Javascript language we cannot fix.

Note: See TracTickets for help on using tickets.