summaryrefslogtreecommitdiff
path: root/js/dojo-1.6/dojox/math/round.xd.js
blob: 5ae88c6805917f30135172606c2d62b63093999c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
	Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


dojo._xdResourceLoaded(function(dojo, dijit, dojox){
return {depends: [["provide", "dojox.math.round"]],
defineResource: function(dojo, dijit, dojox){if(!dojo._hasResource["dojox.math.round"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.math.round"] = true;
dojo.provide("dojox.math.round");


dojo.getObject("math.round", true, dojox);
dojo.experimental("dojox.math.round");

dojox.math.round = function(/*Number*/value, /*Number?*/places, /*Number?*/increment){
	//	summary:
	//		Similar to dojo.number.round, but compensates for binary floating point artifacts
	//	description:
	//		Rounds to the nearest value with the given number of decimal places, away from zero if equal,
	//		similar to Number.toFixed().  Rounding can be done by fractional increments also.
	//		Makes minor adjustments to accommodate for precision errors due to binary floating point representation
	//		of Javascript Numbers.  See http://speleotrove.com/decimal/decifaq.html for more information.
	//		Because of this adjustment, the rounding may not be mathematically correct for full precision
	//		floating point values.  The calculations assume 14 significant figures, so the accuracy will
	//		be limited to a certain number of decimal places preserved will vary with the magnitude of
	//		the input.  This is not a substitute for decimal arithmetic.
	//	value:
	//		The number to round
	//	places:
	//		The number of decimal places where rounding takes place.  Defaults to 0 for whole rounding.
	//		Must be non-negative.
	//	increment:
	//		Rounds next place to nearest value of increment/10.  10 by default.
	//	example:
	//		>>> 4.8-(1.1+2.2)
	//		1.4999999999999996
	//		>>> Math.round(4.8-(1.1+2.2))
	//		1
	//		>>> dojox.math.round(4.8-(1.1+2.2))
	//		2
	//		>>> ((4.8-(1.1+2.2))/100)
	//		0.014999999999999996
	//		>>> ((4.8-(1.1+2.2))/100).toFixed(2)
	//		"0.01"
	//		>>> dojox.math.round((4.8-(1.1+2.2))/100,2)
	//		0.02
	//		>>> dojox.math.round(10.71, 0, 2.5)
	//		10.75
	//		>>> dojo.number.round(162.295, 2)
	//		162.29
	//		>>> dojox.math.round(162.295, 2)
	//		162.3
	var wholeFigs = Math.log(Math.abs(value))/Math.log(10);
	var factor = 10 / (increment || 10);
	var delta = Math.pow(10, -15 + wholeFigs);
	return (factor * (+value + (value > 0 ? delta : -delta))).toFixed(places) / factor; // Number
}

if((0.9).toFixed() == 0){
	// (isIE) toFixed() bug workaround: Rounding fails on IE when most significant digit
	// is just after the rounding place and is >=5
	(function(){
		var round = dojox.math.round;
		dojox.math.round = function(v, p, m){
			var d = Math.pow(10, -p || 0), a = Math.abs(v);
			if(!v || a >= d || a * Math.pow(10, p + 1) < 5){
				d = 0;
			}
			return round(v, p, m) + (v > 0 ? d : -d);
		}
	})();
}

}

}};});