1 //<script type="text/javascript">
3 //+ based on.. Jonas Raoni Soares Silva
4 //@ http://jsfromhell.com/classes/bignumber [rev. #4]
6 Roo = Roo || { util : { }};
9 Roo.util.Math = function(num, precision, roundType){
11 if(num instanceof Roo.util.Math){
13 for(i in {precision: 0, roundType: 0, _s: 0, _f: 0}) o[i] = num[i];
14 this._d = num._d.slice();
18 this.precision = isNaN(precision = Math.abs(precision)) ? Roo.util.Math.defaultPrecision : precision;
19 this.roundType = isNaN(r = Math.abs(roundType)) ? Roo.util.Math.defaultRoundType : roundType;
21 this._s = (num += "").charAt(0) == "-";
23 (num = num.replace(/[^\d.]/g, "").split(".", 2))[0] = num[0].replace(/^0+/, "") || "0"
25 for(i = (num = this._d = (num.join("") || "0").split("")).length; i; num[--i] = +num[i]);
29 Roo.util.Math.ROUND_HALF_EVEN = (Roo.util.Math.ROUND_HALF_DOWN = (Roo.util.Math.ROUND_HALF_UP =
30 (Roo.util.Math.ROUND_FLOOR = (Roo.util.Math.ROUND_CEIL = (Roo.util.Math.ROUND_DOWN
31 = (Roo.util.Math.ROUND_UP = 0) + 1) + 1) + 1) + 1) + 1) + 1;
33 Roo.util.Math.defaultPrecision = 40;
34 Roo.util.Math.defaultRoundType = Roo.util.Math.ROUND_HALF_UP;
37 Roo.util.Math.prototype = {
44 num = new Roo.util.Math(num, this.precision, this.roundType);
46 if (this._s != num._s) { //netagive...
47 return num._s ^= 1, this.subtract(num);
50 var o = new Roo.util.Math(this),
55 num = Math.max(la, lb),
58 la != lb && ((lb = la - lb) > 0 ? o._zeroes(b, lb, 1) : o._zeroes(a, -lb, 1));
59 i = (la = a.length) == (lb = b.length) ?
61 (lb = la - lb) > 0 ? o._zeroes(b, lb) : o._zeroes(a, -lb)
65 r = (a[--i] = a[i] + b[i] + r) / 10 >>> 0,
68 r && ++num && a.unshift(r);
73 subtract : function(n){
74 if(this._s != (n = new Roo.util.Math(n, this.precision, this.roundType))._s)
75 return n._s ^= 1, this.add(n);
76 var o = new Roo.util.Math(this), c = o.abs().compare(n.abs()) + 1, a = c ? o : n, b = c ? n : o, la = a._f, lb = b._f, d = la, i, j;
77 a = a._d, b = b._d, la != lb && ((lb = la - lb) > 0 ? o._zeroes(b, lb, 1) : o._zeroes(a, -lb, 1));
78 for(i = (la = a.length) == (lb = b.length) ? a.length : ((lb = la - lb) > 0 ? o._zeroes(b, lb) : o._zeroes(a, -lb)).length; i;){
80 for(j = i; j && !a[--j]; a[j] = 9);
85 return c || (o._s ^= 1), o._f = d, o._d = b, o.round();
87 multiply : function(n){
88 var o = new Roo.util.Math(this), r = o._d.length >= (n = new Roo.util.Math(n, this.precision, this.roundType))._d.length, a = (r ? o : n)._d,
89 b = (r ? n : o)._d, la = a.length, lb = b.length, x = new Roo.util.Math(0,this.precision, this.roundType), i, j, s;
90 for(i = lb; i; r && s.unshift(r), x.set(x.add(new Roo.util.Math(s.join(""), this.precision, this.roundType))))
91 for(s = (new Array(lb - --i)).join("0").split(""), r = 0, j = la; j; r += a[--j] * b[i], s.unshift(r % 10), r = (r / 10) >>> 0);
92 return o._s = o._s != n._s, o._f = ((r = la + lb - o._f - n._f) >= (j = (o._d = x._d).length) ? this._zeroes(o._d, r - j + 1, 1).length : j) - r, o.round();
95 if((n = new Roo.util.Math(n, this.precision, this.roundType)) == "0")
96 throw new Error("Division by 0");
98 return new Roo.util.Math(0, this.precision, this.roundType);
99 var o = new Roo.util.Math(this), a = o._d, b = n._d, la = a.length - o._f,
100 lb = b.length - n._f, r = new Roo.util.Math(0, this.precision, this.roundType), i = 0, j, s, l, f = 1, c = 0, e = 0;
101 r._s = o._s != n._s, r.precision = Math.max(o.precision, n.precision),
102 r._f = +r._d.pop(), la != lb && o._zeroes(la > lb ? b : a, Math.abs(la - lb));
103 n._f = b.length, b = n, b._s = false, b = b.round();
104 for(n = new Roo.util.Math(0, this.precision, this.roundType); a[0] == "0"; a.shift());
107 for(l = c = 0, n == "0" && (n._d = [], n._f = 0); i < a.length && n.compare(b) == -1; ++i){
108 (l = i + 1 == a.length, (!f && ++c > 1 || (e = l && n == "0" && a[i] == "0")))
109 && (r._f == r._d.length && ++r._f, r._d.push(0));
110 (a[i] == "0" && n == "0") || (n._d.push(a[i]), ++n._f);
113 if((l && n.compare(b) == -1 && (r._f == r._d.length && ++r._f, 1)) || (l = 0))
114 while(r._d.push(0), n._d.push(0), ++n._f, n.compare(b) == -1);
116 if(f = 0, n.compare(b) == -1 && !(l = 0))
117 while(l ? r._d.push(0) : l = 1, n._d.push(0), ++n._f, n.compare(b) == -1);
118 for(s = new Roo.util.Math(0, this.precision, this.roundType), j = 0; n.compare(y = s.add(b)) + 1 && ++j; s.set(y));
119 n.set(n.subtract(s)), !l && r._f == r._d.length && ++r._f, r._d.push(j);
121 while((i < a.length || n != "0") && (r._d.length - r._f) <= r.precision);
125 return this.subtract(this.divide(n).intPart().multiply(n));
128 var o = new Roo.util.Math(this), i;
129 if((n = (new Roo.util.Math(n, this.precision, this.roundType)).intPart()) == 0) return o.set(1);
130 for(i = Math.abs(n); --i; o.set(o.multiply(this)));
131 return n < 0 ? o.set((new Roo.util.Math(1, this.precision, this.roundType)).divide(o)) : o;
134 return this.constructor(n), this;
136 compare : function(n){
137 var a = this, la = this._f, b = new Roo.util.Math(n, this.precision, this.roundType), lb = b._f, r = [-1, 1], i, l;
139 return a._s ? -1 : 1;
141 return r[(la > lb) ^ a._s];
142 for(la = (a = a._d).length, lb = (b = b._d).length, i = -1, l = Math.min(la, lb); ++i < l;)
144 return r[(a[i] > b[i]) ^ a._s];
145 return la != lb ? r[(la > lb) ^ a._s] : 0;
148 var n = new Roo.util.Math(this);
152 var n = new Roo.util.Math(this);
155 intPart : function(){
156 return new Roo.util.Math((this._s ? "-" : "", this.precision, this.roundType) + (this._d.slice(0, this._f).join("") || "0"));
158 valueOf : function() {
159 return this.toString();
161 toString : function(){
163 return (o._s ? "-" : "") + (o._d.slice(0, o._f).join("") || "0") + (o._f != o._d.length ? "." + o._d.slice(o._f).join("") : "");
165 toFixed : function(x) {
166 var n = new Roo.util.Math(this);
168 return n.round().toString();
170 _zeroes : function(n, l, t){
171 var s = ["push", "unshift"][t || 0];
172 for(++l; --l; n[s](0));
178 if("_rounding" in this) return this; // stop recursion..
180 var r = this.roundType,
183 console.log(b.join(','));
184 for(this._rounding = true;
185 this._f > 1 && !b[0];
188 console.log(b.join(','));
190 p = this.precision + d,
193 b.length > d && !b[b.length -1]; // condition...
198 x = (this._s ? "-" : "") + (p - d ? "0." + this._zeroes([], p - d - 1).join("") : "") + 1;
200 var m = Roo.util.Math;
202 if(b.length > p && n){
203 console.log("rounding" +n + " Method? " + r);
206 (r == m.CEIL && !this._s) ||
207 (r == m.FLOOR && this._s) ||
208 (r == m.HALF_UP && ? n >= 5) ||
209 (r == m.HALF_DOWN && ? n > 5) ||
210 (r == m.HALF_EVEN && n >= 5 && (b[p - 1] & 1)) {
211 console.log("add" +x);
218 b.splice(p, b.length - p);
220 return delete this._rounding, this;