source: Dev/trunk/src/client/dojox/math/round.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 2.6 KB
Line 
1// AMD-ID "dojox/math/round"
2define(["dojo", "dojox"], function(dojo, dojox) {
3
4        dojo.getObject("math.round", true, dojox);
5        dojo.experimental("dojox.math.round");
6
7        dojox.math.round = function(/*Number*/value, /*Number?*/places, /*Number?*/increment){
8                // summary:
9                //              Similar to dojo.number.round, but compensates for binary floating point artifacts
10                // description:
11                //              Rounds to the nearest value with the given number of decimal places, away from zero if equal,
12                //              similar to Number.toFixed().  Rounding can be done by fractional increments also.
13                //              Makes minor adjustments to accommodate for precision errors due to binary floating point representation
14                //              of Javascript Numbers.  See http://speleotrove.com/decimal/decifaq.html for more information.
15                //              Because of this adjustment, the rounding may not be mathematically correct for full precision
16                //              floating point values.  The calculations assume 14 significant figures, so the accuracy will
17                //              be limited to a certain number of decimal places preserved will vary with the magnitude of
18                //              the input.  This is not a substitute for decimal arithmetic.
19                // value:
20                //              The number to round
21                // places:
22                //              The number of decimal places where rounding takes place.  Defaults to 0 for whole rounding.
23                //              Must be non-negative.
24                // increment:
25                //              Rounds next place to nearest value of increment/10.  10 by default.
26                // example:
27                //      |       >>> 4.8-(1.1+2.2)
28                //      |       1.4999999999999996
29                //      |       >>> Math.round(4.8-(1.1+2.2))
30                //      |       1
31                //      |       >>> dojox.math.round(4.8-(1.1+2.2))
32                //      |       2
33                //      |       >>> ((4.8-(1.1+2.2))/100)
34                //      |       0.014999999999999996
35                //      |       >>> ((4.8-(1.1+2.2))/100).toFixed(2)
36                //      |       "0.01"
37                //      |       >>> dojox.math.round((4.8-(1.1+2.2))/100,2)
38                //      |       0.02
39                //      |       >>> dojox.math.round(10.71, 0, 2.5)
40                //      |       10.75
41                //      |       >>> dojo.number.round(162.295, 2)
42                //      |       162.29
43                //      |       >>> dojox.math.round(162.295, 2)
44                //      |       162.3
45                var wholeFigs = Math.log(Math.abs(value))/Math.log(10);
46                var factor = 10 / (increment || 10);
47                var delta = Math.pow(10, -15 + wholeFigs);
48                return (factor * (+value + (value > 0 ? delta : -delta))).toFixed(places) / factor; // Number
49        }
50
51        if((0.9).toFixed() == 0){
52                // (isIE) toFixed() bug workaround: Rounding fails on IE when most significant digit
53                // is just after the rounding place and is >=5
54                var round = dojox.math.round;
55                dojox.math.round = function(v, p, m){
56                        var d = Math.pow(10, -p || 0), a = Math.abs(v);
57                        if(!v || a >= d){
58                                d = 0;
59                        }else{
60                                a /= d;
61                                if(a < 0.5 || a >= 0.95){
62                                        d = 0;
63                                }
64                        }
65                        return round(v, p, m) + (v > 0 ? d : -d);
66                }
67        }
68
69        return dojox.math.round;
70});
Note: See TracBrowser for help on using the repository browser.