1
|
/*
|
2
|
Copyright (c) 2004-2008, 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
|
5
|
*/
|
6
|
|
7
|
|
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,"zh-cn,zh,ko-kr,pt,en-us,en-gb,de,ja,ja-jp,en,ROOT,en-au,fr,es,ko,zh-tw,it,es-es,de-de");
|
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);
|
18
|
var _4=dojo.i18n.getLocalization("dojo.cldr","number",_3);
|
19
|
_2.customs=_4;
|
20
|
var _5=_2.pattern||_4[(_2.type||"decimal")+"Format"];
|
21
|
if(isNaN(_1)){
|
22
|
return null;
|
23
|
}
|
24
|
return dojo.number._applyPattern(_1,_5,_2);
|
25
|
};
|
26
|
dojo.number._numberPatternRE=/[#0,]*[#0](?:\.0*#*)?/;
|
27
|
dojo.number._applyPattern=function(_6,_7,_8){
|
28
|
_8=_8||{};
|
29
|
var _9=_8.customs.group;
|
30
|
var _a=_8.customs.decimal;
|
31
|
var _b=_7.split(";");
|
32
|
var _c=_b[0];
|
33
|
_7=_b[(_6<0)?1:0]||("-"+_c);
|
34
|
if(_7.indexOf("%")!=-1){
|
35
|
_6*=100;
|
36
|
}else{
|
37
|
if(_7.indexOf("‰")!=-1){
|
38
|
_6*=1000;
|
39
|
}else{
|
40
|
if(_7.indexOf("¤")!=-1){
|
41
|
_9=_8.customs.currencyGroup||_9;
|
42
|
_a=_8.customs.currencyDecimal||_a;
|
43
|
_7=_7.replace(/\u00a4{1,3}/,function(_d){
|
44
|
var _e=["symbol","currency","displayName"][_d.length-1];
|
45
|
return _8[_e]||_8.currency||"";
|
46
|
});
|
47
|
}else{
|
48
|
if(_7.indexOf("E")!=-1){
|
49
|
throw new Error("exponential notation not supported");
|
50
|
}
|
51
|
}
|
52
|
}
|
53
|
}
|
54
|
var _f=dojo.number._numberPatternRE;
|
55
|
var _10=_c.match(_f);
|
56
|
if(!_10){
|
57
|
throw new Error("unable to find a number expression in pattern: "+_7);
|
58
|
}
|
59
|
if(_8.fractional===false){
|
60
|
_8.places=0;
|
61
|
}
|
62
|
return _7.replace(_f,dojo.number._formatAbsolute(_6,_10[0],{decimal:_a,group:_9,places:_8.places,round:_8.round}));
|
63
|
};
|
64
|
dojo.number.round=function(_11,_12,_13){
|
65
|
var _14=String(_11).split(".");
|
66
|
var _15=(_14[1]&&_14[1].length)||0;
|
67
|
if(_15>_12){
|
68
|
var _16=Math.pow(10,_12);
|
69
|
if(_13>0){
|
70
|
_16*=10/_13;
|
71
|
_12++;
|
72
|
}
|
73
|
_11=Math.round(_11*_16)/_16;
|
74
|
_14=String(_11).split(".");
|
75
|
_15=(_14[1]&&_14[1].length)||0;
|
76
|
if(_15>_12){
|
77
|
_14[1]=_14[1].substr(0,_12);
|
78
|
_11=Number(_14.join("."));
|
79
|
}
|
80
|
}
|
81
|
return _11;
|
82
|
};
|
83
|
dojo.number._formatAbsolute=function(_17,_18,_19){
|
84
|
_19=_19||{};
|
85
|
if(_19.places===true){
|
86
|
_19.places=0;
|
87
|
}
|
88
|
if(_19.places===Infinity){
|
89
|
_19.places=6;
|
90
|
}
|
91
|
var _1a=_18.split(".");
|
92
|
var _1b=(_19.places>=0)?_19.places:(_1a[1]&&_1a[1].length)||0;
|
93
|
if(!(_19.round<0)){
|
94
|
_17=dojo.number.round(_17,_1b,_19.round);
|
95
|
}
|
96
|
var _1c=String(Math.abs(_17)).split(".");
|
97
|
var _1d=_1c[1]||"";
|
98
|
if(_19.places){
|
99
|
var _1e=dojo.isString(_19.places)&&_19.places.indexOf(",");
|
100
|
if(_1e){
|
101
|
_19.places=_19.places.substring(_1e+1);
|
102
|
}
|
103
|
_1c[1]=dojo.string.pad(_1d.substr(0,_19.places),_19.places,"0",true);
|
104
|
}else{
|
105
|
if(_1a[1]&&_19.places!==0){
|
106
|
var pad=_1a[1].lastIndexOf("0")+1;
|
107
|
if(pad>_1d.length){
|
108
|
_1c[1]=dojo.string.pad(_1d,pad,"0",true);
|
109
|
}
|
110
|
var _20=_1a[1].length;
|
111
|
if(_20<_1d.length){
|
112
|
_1c[1]=_1d.substr(0,_20);
|
113
|
}
|
114
|
}else{
|
115
|
if(_1c[1]){
|
116
|
_1c.pop();
|
117
|
}
|
118
|
}
|
119
|
}
|
120
|
var _21=_1a[0].replace(",","");
|
121
|
pad=_21.indexOf("0");
|
122
|
if(pad!=-1){
|
123
|
pad=_21.length-pad;
|
124
|
if(pad>_1c[0].length){
|
125
|
_1c[0]=dojo.string.pad(_1c[0],pad);
|
126
|
}
|
127
|
if(_21.indexOf("#")==-1){
|
128
|
_1c[0]=_1c[0].substr(_1c[0].length-pad);
|
129
|
}
|
130
|
}
|
131
|
var _22=_1a[0].lastIndexOf(",");
|
132
|
var _23,_24;
|
133
|
if(_22!=-1){
|
134
|
_23=_1a[0].length-_22-1;
|
135
|
var _25=_1a[0].substr(0,_22);
|
136
|
_22=_25.lastIndexOf(",");
|
137
|
if(_22!=-1){
|
138
|
_24=_25.length-_22-1;
|
139
|
}
|
140
|
}
|
141
|
var _26=[];
|
142
|
for(var _27=_1c[0];_27;){
|
143
|
var off=_27.length-_23;
|
144
|
_26.push((off>0)?_27.substr(off):_27);
|
145
|
_27=(off>0)?_27.slice(0,off):"";
|
146
|
if(_24){
|
147
|
_23=_24;
|
148
|
delete _24;
|
149
|
}
|
150
|
}
|
151
|
_1c[0]=_26.reverse().join(_19.group||",");
|
152
|
return _1c.join(_19.decimal||".");
|
153
|
};
|
154
|
dojo.number.regexp=function(_29){
|
155
|
return dojo.number._parseInfo(_29).regexp;
|
156
|
};
|
157
|
dojo.number._parseInfo=function(_2a){
|
158
|
_2a=_2a||{};
|
159
|
var _2b=dojo.i18n.normalizeLocale(_2a.locale);
|
160
|
var _2c=dojo.i18n.getLocalization("dojo.cldr","number",_2b);
|
161
|
var _2d=_2a.pattern||_2c[(_2a.type||"decimal")+"Format"];
|
162
|
var _2e=_2c.group;
|
163
|
var _2f=_2c.decimal;
|
164
|
var _30=1;
|
165
|
if(_2d.indexOf("%")!=-1){
|
166
|
_30/=100;
|
167
|
}else{
|
168
|
if(_2d.indexOf("‰")!=-1){
|
169
|
_30/=1000;
|
170
|
}else{
|
171
|
var _31=_2d.indexOf("¤")!=-1;
|
172
|
if(_31){
|
173
|
_2e=_2c.currencyGroup||_2e;
|
174
|
_2f=_2c.currencyDecimal||_2f;
|
175
|
}
|
176
|
}
|
177
|
}
|
178
|
var _32=_2d.split(";");
|
179
|
if(_32.length==1){
|
180
|
_32.push("-"+_32[0]);
|
181
|
}
|
182
|
var re=dojo.regexp.buildGroupRE(_32,function(_34){
|
183
|
_34="(?:"+dojo.regexp.escapeString(_34,".")+")";
|
184
|
return _34.replace(dojo.number._numberPatternRE,function(_35){
|
185
|
var _36={signed:false,separator:_2a.strict?_2e:[_2e,""],fractional:_2a.fractional,decimal:_2f,exponent:false};
|
186
|
var _37=_35.split(".");
|
187
|
var _38=_2a.places;
|
188
|
if(_37.length==1||_38===0){
|
189
|
_36.fractional=false;
|
190
|
}else{
|
191
|
if(_38===undefined){
|
192
|
_38=_2a.pattern?_37[1].lastIndexOf("0")+1:Infinity;
|
193
|
}
|
194
|
if(_38&&_2a.fractional==undefined){
|
195
|
_36.fractional=true;
|
196
|
}
|
197
|
if(!_2a.places&&(_38<_37[1].length)){
|
198
|
_38+=","+_37[1].length;
|
199
|
}
|
200
|
_36.places=_38;
|
201
|
}
|
202
|
var _39=_37[0].split(",");
|
203
|
if(_39.length>1){
|
204
|
_36.groupSize=_39.pop().length;
|
205
|
if(_39.length>1){
|
206
|
_36.groupSize2=_39.pop().length;
|
207
|
}
|
208
|
}
|
209
|
return "("+dojo.number._realNumberRegexp(_36)+")";
|
210
|
});
|
211
|
},true);
|
212
|
if(_31){
|
213
|
re=re.replace(/([\s\xa0]*)(\u00a4{1,3})([\s\xa0]*)/g,function(_3a,_3b,_3c,_3d){
|
214
|
var _3e=["symbol","currency","displayName"][_3c.length-1];
|
215
|
var _3f=dojo.regexp.escapeString(_2a[_3e]||_2a.currency||"");
|
216
|
_3b=_3b?"[\\s\\xa0]":"";
|
217
|
_3d=_3d?"[\\s\\xa0]":"";
|
218
|
if(!_2a.strict){
|
219
|
if(_3b){
|
220
|
_3b+="*";
|
221
|
}
|
222
|
if(_3d){
|
223
|
_3d+="*";
|
224
|
}
|
225
|
return "(?:"+_3b+_3f+_3d+")?";
|
226
|
}
|
227
|
return _3b+_3f+_3d;
|
228
|
});
|
229
|
}
|
230
|
return {regexp:re.replace(/[\xa0 ]/g,"[\\s\\xa0]"),group:_2e,decimal:_2f,factor:_30};
|
231
|
};
|
232
|
dojo.number.parse=function(_40,_41){
|
233
|
var _42=dojo.number._parseInfo(_41);
|
234
|
var _43=(new RegExp("^"+_42.regexp+"$")).exec(_40);
|
235
|
if(!_43){
|
236
|
return NaN;
|
237
|
}
|
238
|
var _44=_43[1];
|
239
|
if(!_43[1]){
|
240
|
if(!_43[2]){
|
241
|
return NaN;
|
242
|
}
|
243
|
_44=_43[2];
|
244
|
_42.factor*=-1;
|
245
|
}
|
246
|
_44=_44.replace(new RegExp("["+_42.group+"\\s\\xa0"+"]","g"),"").replace(_42.decimal,".");
|
247
|
return Number(_44)*_42.factor;
|
248
|
};
|
249
|
dojo.number._realNumberRegexp=function(_45){
|
250
|
_45=_45||{};
|
251
|
if(!("places" in _45)){
|
252
|
_45.places=Infinity;
|
253
|
}
|
254
|
if(typeof _45.decimal!="string"){
|
255
|
_45.decimal=".";
|
256
|
}
|
257
|
if(!("fractional" in _45)||/^0/.test(_45.places)){
|
258
|
_45.fractional=[true,false];
|
259
|
}
|
260
|
if(!("exponent" in _45)){
|
261
|
_45.exponent=[true,false];
|
262
|
}
|
263
|
if(!("eSigned" in _45)){
|
264
|
_45.eSigned=[true,false];
|
265
|
}
|
266
|
var _46=dojo.number._integerRegexp(_45);
|
267
|
var _47=dojo.regexp.buildGroupRE(_45.fractional,function(q){
|
268
|
var re="";
|
269
|
if(q&&(_45.places!==0)){
|
270
|
re="\\"+_45.decimal;
|
271
|
if(_45.places==Infinity){
|
272
|
re="(?:"+re+"\\d+)?";
|
273
|
}else{
|
274
|
re+="\\d{"+_45.places+"}";
|
275
|
}
|
276
|
}
|
277
|
return re;
|
278
|
},true);
|
279
|
var _4a=dojo.regexp.buildGroupRE(_45.exponent,function(q){
|
280
|
if(q){
|
281
|
return "([eE]"+dojo.number._integerRegexp({signed:_45.eSigned})+")";
|
282
|
}
|
283
|
return "";
|
284
|
});
|
285
|
var _4c=_46+_47;
|
286
|
if(_47){
|
287
|
_4c="(?:(?:"+_4c+")|(?:"+_47+"))";
|
288
|
}
|
289
|
return _4c+_4a;
|
290
|
};
|
291
|
dojo.number._integerRegexp=function(_4d){
|
292
|
_4d=_4d||{};
|
293
|
if(!("signed" in _4d)){
|
294
|
_4d.signed=[true,false];
|
295
|
}
|
296
|
if(!("separator" in _4d)){
|
297
|
_4d.separator="";
|
298
|
}else{
|
299
|
if(!("groupSize" in _4d)){
|
300
|
_4d.groupSize=3;
|
301
|
}
|
302
|
}
|
303
|
var _4e=dojo.regexp.buildGroupRE(_4d.signed,function(q){
|
304
|
return q?"[-+]":"";
|
305
|
},true);
|
306
|
var _50=dojo.regexp.buildGroupRE(_4d.separator,function(sep){
|
307
|
if(!sep){
|
308
|
return "(?:0|[1-9]\\d*)";
|
309
|
}
|
310
|
sep=dojo.regexp.escapeString(sep);
|
311
|
if(sep==" "){
|
312
|
sep="\\s";
|
313
|
}else{
|
314
|
if(sep==" "){
|
315
|
sep="\\s\\xa0";
|
316
|
}
|
317
|
}
|
318
|
var grp=_4d.groupSize,_53=_4d.groupSize2;
|
319
|
if(_53){
|
320
|
var _54="(?:0|[1-9]\\d{0,"+(_53-1)+"}(?:["+sep+"]\\d{"+_53+"})*["+sep+"]\\d{"+grp+"})";
|
321
|
return ((grp-_53)>0)?"(?:"+_54+"|(?:0|[1-9]\\d{0,"+(grp-1)+"}))":_54;
|
322
|
}
|
323
|
return "(?:0|[1-9]\\d{0,"+(grp-1)+"}(?:["+sep+"]\\d{"+grp+"})*)";
|
324
|
},true);
|
325
|
return _4e+_50;
|
326
|
};
|
327
|
}
|