]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/number.js
2 Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
8 if(!dojo
._hasResource
["dojo.number"]){
9 dojo
._hasResource
["dojo.number"]=true;
10 dojo
.provide("dojo.number");
11 dojo
.require("dojo.i18n");
12 dojo
.requireLocalization("dojo.cldr","number",null,"ROOT,ar,ca,cs,da,de,el,en,en-au,en-gb,es,fi,fr,he,hu,it,ja,ko,nb,nl,pl,pt,pt-pt,ru,sk,sl,sv,th,tr,zh");
13 dojo
.require("dojo.string");
14 dojo
.require("dojo.regexp");
15 dojo
.number
.format=function(_1
,_2
){
16 _2
=dojo
.mixin({},_2
||{});
17 var _3
=dojo
.i18n
.normalizeLocale(_2
.locale
),_4
=dojo
.i18n
.getLocalization("dojo.cldr","number",_3
);
19 var _5
=_2
.pattern
||_4
[(_2
.type
||"decimal")+"Format"];
20 if(isNaN(_1
)||Math
.abs(_1
)==Infinity
){
23 return dojo
.number
._applyPattern(_1
,_5
,_2
);
25 dojo
.number
._numberPatternRE
=/[#0,]*[#0](?:\.0*#*)?/;
26 dojo
.number
._applyPattern=function(_6
,_7
,_8
){
28 var _9
=_8
.customs
.group
,_a
=_8
.customs
.decimal,_b
=_7
.split(";"),_c
=_b
[0];
29 _7
=_b
[(_6
<0)?1:0]||("-"+_c
);
30 if(_7
.indexOf("%")!=-1){
33 if(_7
.indexOf("‰")!=-1){
36 if(_7
.indexOf("¤")!=-1){
37 _9
=_8
.customs
.currencyGroup
||_9
;
38 _a
=_8
.customs
.currencyDecimal
||_a
;
39 _7
=_7
.replace(/\u00a4{1,3}/,function(_d
){
40 var _e
=["symbol","currency","displayName"][_d
.length
-1];
41 return _8
[_e
]||_8
.currency
||"";
44 if(_7
.indexOf("E")!=-1){
45 throw new Error("exponential notation not supported");
50 var _f
=dojo
.number
._numberPatternRE
;
53 throw new Error("unable to find a number expression in pattern: "+_7
);
55 if(_8
.fractional
===false){
58 return _7
.replace(_f
,dojo
.number
._formatAbsolute(_6
,_10
[0],{decimal:_a
,group
:_9
,places
:_8
.places
,round
:_8
.round
}));
60 dojo
.number
.round=function(_11
,_12
,_13
){
62 return (_14
*+_11
).toFixed(_12
)/_14
;
64 if((0.9).toFixed()==0){
66 var _15
=dojo
.number
.round
;
67 dojo
.number
.round=function(v
,p
,m
){
68 var d
=Math
.pow(10,-p
||0),a
=Math
.abs(v
);
69 if(!v
||a
>=d
||a
*Math
.pow(10,p
+1)<5){
72 return _15(v
,p
,m
)+(v
>0?d
:-d
);
76 dojo
.number
._formatAbsolute=function(_16
,_17
,_18
){
78 if(_18
.places
===true){
81 if(_18
.places
===Infinity
){
84 var _19
=_17
.split("."),_1a
=typeof _18
.places
=="string"&&_18
.places
.indexOf(","),_1b
=_18
.places
;
86 _1b
=_18
.places
.substring(_1a
+1);
89 _1b
=(_19
[1]||[]).length
;
93 _16
=dojo
.number
.round(_16
,_1b
,_18
.round
);
95 var _1c
=String(Math
.abs(_16
)).split("."),_1d
=_1c
[1]||"";
96 if(_19
[1]||_18
.places
){
98 _18
.places
=_18
.places
.substring(0,_1a
);
100 var pad
=_18
.places
!==undefined?_18
.places
:(_19
[1]&&_19
[1].lastIndexOf("0")+1);
102 _1c
[1]=dojo
.string
.pad(_1d
,pad
,"0",true);
105 _1c
[1]=_1d
.substr(0,_1b
);
112 var _1e
=_19
[0].replace(",","");
113 pad
=_1e
.indexOf("0");
116 if(pad
>_1c
[0].length
){
117 _1c
[0]=dojo
.string
.pad(_1c
[0],pad
);
119 if(_1e
.indexOf("#")==-1){
120 _1c
[0]=_1c
[0].substr(_1c
[0].length
-pad
);
123 var _1f
=_19
[0].lastIndexOf(","),_20
,_21
;
125 _20
=_19
[0].length
-_1f
-1;
126 var _22
=_19
[0].substr(0,_1f
);
127 _1f
=_22
.lastIndexOf(",");
129 _21
=_22
.length
-_1f
-1;
133 for(var _24
=_1c
[0];_24
;){
134 var off
=_24
.length
-_20
;
135 _23
.push((off
>0)?_24
.substr(off
):_24
);
136 _24
=(off
>0)?_24
.slice(0,off
):"";
142 _1c
[0]=_23
.reverse().join(_18
.group
||",");
143 return _1c
.join(_18
.decimal||".");
145 dojo
.number
.regexp=function(_25
){
146 return dojo
.number
._parseInfo(_25
).regexp
;
148 dojo
.number
._parseInfo=function(_26
){
150 var _27
=dojo
.i18n
.normalizeLocale(_26
.locale
),_28
=dojo
.i18n
.getLocalization("dojo.cldr","number",_27
),_29
=_26
.pattern
||_28
[(_26
.type
||"decimal")+"Format"],_2a
=_28
.group
,_2b
=_28
.decimal,_2c
=1;
151 if(_29
.indexOf("%")!=-1){
154 if(_29
.indexOf("‰")!=-1){
157 var _2d
=_29
.indexOf("¤")!=-1;
159 _2a
=_28
.currencyGroup
||_2a
;
160 _2b
=_28
.currencyDecimal
||_2b
;
164 var _2e
=_29
.split(";");
166 _2e
.push("-"+_2e
[0]);
168 var re
=dojo
.regexp
.buildGroupRE(_2e
,function(_2f
){
169 _2f
="(?:"+dojo
.regexp
.escapeString(_2f
,".")+")";
170 return _2f
.replace(dojo
.number
._numberPatternRE
,function(_30
){
171 var _31
={signed
:false,separator
:_26
.strict
?_2a
:[_2a
,""],fractional
:_26
.fractional
,decimal:_2b
,exponent
:false},_32
=_30
.split("."),_33
=_26
.places
;
172 if(_32
.length
==1&&_2c
!=1){
175 if(_32
.length
==1||_33
===0){
176 _31
.fractional
=false;
179 _33
=_26
.pattern
?_32
[1].lastIndexOf("0")+1:Infinity
;
181 if(_33
&&_26
.fractional
==undefined){
184 if(!_26
.places
&&(_33
<_32
[1].length
)){
185 _33
+=","+_32
[1].length
;
189 var _34
=_32
[0].split(",");
191 _31
.groupSize
=_34
.pop().length
;
193 _31
.groupSize2
=_34
.pop().length
;
196 return "("+dojo
.number
._realNumberRegexp(_31
)+")";
200 re
=re
.replace(/([\s\xa0]*)(\u00a4{1,3})([\s\xa0]*)/g,function(_35
,_36
,_37
,_38
){
201 var _39
=["symbol","currency","displayName"][_37
.length
-1],_3a
=dojo
.regexp
.escapeString(_26
[_39
]||_26
.currency
||"");
202 _36
=_36
?"[\\s\\xa0]":"";
203 _38
=_38
?"[\\s\\xa0]":"";
211 return "(?:"+_36
+_3a
+_38
+")?";
216 return {regexp
:re
.replace(/[\xa0 ]/g,"[\\s\\xa0]"),group
:_2a
,decimal:_2b
,factor
:_2c
};
218 dojo
.number
.parse=function(_3b
,_3c
){
219 var _3d
=dojo
.number
._parseInfo(_3c
),_3e
=(new RegExp("^"+_3d
.regexp
+"$")).exec(_3b
);
231 _3f
=_3f
.replace(new RegExp("["+_3d
.group
+"\\s\\xa0"+"]","g"),"").replace(_3d
.decimal,".");
232 return _3f
*_3d
.factor
;
234 dojo
.number
._realNumberRegexp=function(_40
){
236 if(!("places" in _40
)){
239 if(typeof _40
.decimal!="string"){
242 if(!("fractional" in _40
)||/^0/.test(_40
.places
)){
243 _40
.fractional
=[true,false];
245 if(!("exponent" in _40
)){
246 _40
.exponent
=[true,false];
248 if(!("eSigned" in _40
)){
249 _40
.eSigned
=[true,false];
251 var _41
=dojo
.number
._integerRegexp(_40
),_42
=dojo
.regexp
.buildGroupRE(_40
.fractional
,function(q
){
253 if(q
&&(_40
.places
!==0)){
255 if(_40
.places
==Infinity
){
256 re
="(?:"+re
+"\\d+)?";
258 re
+="\\d{"+_40
.places
+"}";
263 var _43
=dojo
.regexp
.buildGroupRE(_40
.exponent
,function(q
){
265 return "([eE]"+dojo
.number
._integerRegexp({signed
:_40
.eSigned
})+")";
271 _44
="(?:(?:"+_44
+")|(?:"+_42
+"))";
275 dojo
.number
._integerRegexp=function(_45
){
277 if(!("signed" in _45
)){
278 _45
.signed
=[true,false];
280 if(!("separator" in _45
)){
283 if(!("groupSize" in _45
)){
287 var _46
=dojo
.regexp
.buildGroupRE(_45
.signed
,function(q
){
290 var _47
=dojo
.regexp
.buildGroupRE(_45
.separator
,function(sep
){
294 sep
=dojo
.regexp
.escapeString(sep
);
302 var grp
=_45
.groupSize
,_48
=_45
.groupSize2
;
304 var _49
="(?:0|[1-9]\\d{0,"+(_48
-1)+"}(?:["+sep
+"]\\d{"+_48
+"})*["+sep
+"]\\d{"+grp
+"})";
305 return ((grp
-_48
)>0)?"(?:"+_49
+"|(?:0|[1-9]\\d{0,"+(grp
-1)+"}))":_49
;
307 return "(?:0|[1-9]\\d{0,"+(grp
-1)+"}(?:["+sep
+"]\\d{"+grp
+"})*)";