Subversion Repositories Applications.papyrus

Rev

Rev 1372 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.i18n.number");
14
dojo.require("dojo.experimental");
15
dojo.experimental("dojo.i18n.number");
16
dojo.require("dojo.regexp");
17
dojo.require("dojo.i18n.common");
18
dojo.require("dojo.lang.common");
19
dojo.i18n.number.format = function (value, flags, locale) {
20
	flags = (typeof flags == "object") ? flags : {};
21
	var formatData = dojo.i18n.number._mapToLocalizedFormatData(dojo.i18n.number.FORMAT_TABLE, locale);
22
	if (typeof flags.separator == "undefined") {
23
		flags.separator = formatData[1];
24
	}
25
	if (typeof flags.decimal == "undefined") {
26
		flags.decimal = formatData[2];
27
	}
28
	if (typeof flags.groupSize == "undefined") {
29
		flags.groupSize = formatData[3];
30
	}
31
	if (typeof flags.groupSize2 == "undefined") {
32
		flags.groupSize2 = formatData[4];
33
	}
34
	if (typeof flags.round == "undefined") {
35
		flags.round = true;
36
	}
37
	if (typeof flags.signed == "undefined") {
38
		flags.signed = true;
39
	}
40
	var output = (flags.signed && (value < 0)) ? "-" : "";
41
	value = Math.abs(value);
42
	var whole = String((((flags.places > 0) || !flags.round) ? Math.floor : Math.round)(value));
43
	function splitSubstrings(str, count) {
44
		for (var subs = []; str.length >= count; str = str.substr(0, str.length - count)) {
45
			subs.push(str.substr(-count));
46
		}
47
		if (str.length > 0) {
48
			subs.push(str);
49
		}
50
		return subs.reverse();
51
	}
52
	if (flags.groupSize2 && (whole.length > flags.groupSize)) {
53
		var groups = splitSubstrings(whole.substr(0, whole.length - flags.groupSize), flags.groupSize2);
54
		groups.push(whole.substr(-flags.groupSize));
55
		output = output + groups.join(flags.separator);
56
	} else {
57
		if (flags.groupSize) {
58
			output = output + splitSubstrings(whole, flags.groupSize).join(flags.separator);
59
		} else {
60
			output = output + whole;
61
		}
62
	}
63
	if (flags.places > 0) {
64
		var fract = value - Math.floor(value);
65
		fract = (flags.round ? Math.round : Math.floor)(fract * Math.pow(10, flags.places));
66
		output = output + flags.decimal + fract;
67
	}
68
	return output;
69
};
70
dojo.i18n.number.parse = function (value, locale, flags) {
71
	flags = (typeof flags == "object") ? flags : {};
72
	var formatData = dojo.i18n.number._mapToLocalizedFormatData(dojo.i18n.number.FORMAT_TABLE, locale);
73
	if (typeof flags.separator == "undefined") {
74
		flags.separator = formatData[1];
75
	}
76
	if (typeof flags.decimal == "undefined") {
77
		flags.decimal = formatData[2];
78
	}
79
	if (typeof flags.groupSize == "undefined") {
80
		flags.groupSize = formatData[3];
81
	}
82
	if (typeof flags.groupSize2 == "undefined") {
83
		flags.groupSize2 = formatData[4];
84
	}
85
	if (typeof flags.validate == "undefined") {
86
		flags.validate = true;
87
	}
88
	if (flags.validate && !dojo.i18n.number.isReal(value, locale, flags)) {
89
		return Number.NaN;
90
	}
91
	var numbers = value.split(flags.decimal);
92
	if (numbers.length > 2) {
93
		return Number.NaN;
94
	}
95
	var whole = Number(numbers[0].replace(new RegExp("\\" + flags.separator, "g"), ""));
96
	var fract = (numbers.length == 1) ? 0 : Number(numbers[1]) / Math.pow(10, String(numbers[1]).length);
97
	return whole + fract;
98
};
99
dojo.i18n.number.isInteger = function (value, locale, flags) {
100
	flags = (typeof flags == "object") ? flags : {};
101
	var formatData = dojo.i18n.number._mapToLocalizedFormatData(dojo.i18n.number.FORMAT_TABLE, locale);
102
	if (typeof flags.separator == "undefined") {
103
		flags.separator = formatData[1];
104
	} else {
105
		if (dojo.lang.isArray(flags.separator) && flags.separator.length === 0) {
106
			flags.separator = [formatData[1], ""];
107
		}
108
	}
109
	if (typeof flags.groupSize == "undefined") {
110
		flags.groupSize = formatData[3];
111
	}
112
	if (typeof flags.groupSize2 == "undefined") {
113
		flags.groupSize2 = formatData[4];
114
	}
115
	var re = new RegExp("^" + dojo.regexp.integer(flags) + "$");
116
	return re.test(value);
117
};
118
dojo.i18n.number.isReal = function (value, locale, flags) {
119
	flags = (typeof flags == "object") ? flags : {};
120
	var formatData = dojo.i18n.number._mapToLocalizedFormatData(dojo.i18n.number.FORMAT_TABLE, locale);
121
	if (typeof flags.separator == "undefined") {
122
		flags.separator = formatData[1];
123
	} else {
124
		if (dojo.lang.isArray(flags.separator) && flags.separator.length === 0) {
125
			flags.separator = [formatData[1], ""];
126
		}
127
	}
128
	if (typeof flags.decimal == "undefined") {
129
		flags.decimal = formatData[2];
130
	}
131
	if (typeof flags.groupSize == "undefined") {
132
		flags.groupSize = formatData[3];
133
	}
134
	if (typeof flags.groupSize2 == "undefined") {
135
		flags.groupSize2 = formatData[4];
136
	}
137
	var re = new RegExp("^" + dojo.regexp.realNumber(flags) + "$");
138
	return re.test(value);
139
};
140
(function () {
141
	dojo.i18n.number.FORMAT_TABLE = {"ar-ae":["", "", ",", 1], "ar-bh":["", "", ",", 1], "ar-dz":["", "", ",", 1], "ar-eg":["", "", ",", 1], "ar-jo":["", "", ",", 1], "ar-kw":["", "", ",", 1], "ar-lb":["", "", ",", 1], "ar-ma":["", "", ",", 1], "ar-om":["", "", ",", 1], "ar-qa":["", "", ",", 1], "ar-sa":["", "", ",", 1], "ar-sy":["", "", ",", 1], "ar-tn":["", "", ",", 1], "ar-ye":["", "", ",", 1], "cs-cz":[".", ".", ",", 3], "da-dk":[".", ".", ",", 3], "de-at":[".", ".", ",", 3], "de-de":[".", ".", ",", 3], "de-lu":[".", ".", ",", 3], "de-ch":["'", "'", ".", 3], "el-gr":[".", ".", ",", 3], "en-au":[",", ",", ".", 3], "en-ca":[",", ",", ".", 3], "en-gb":[",", ",", ".", 3], "en-hk":[",", ",", ".", 3], "en-ie":[",", ",", ".", 3], "en-in":[",", ",", ".", 3, 2], "en-nz":[",", ",", ".", 3], "en-us":[",", ",", ".", 3], "en-za":[",", ",", ".", 3], "es-ar":[".", ".", ",", 3], "es-bo":[".", ".", ",", 3], "es-cl":[".", ".", ",", 3], "es-co":[".", ".", ",", 3], "es-cr":[".", ".", ",", 3], "es-do":[".", ".", ",", 3], "es-ec":[".", ".", ",", 3], "es-es":[".", ".", ",", 3], "es-gt":[",", ",", ".", 3], "es-hn":[",", ",", ".", 3], "es-mx":[",", ",", ".", 3], "es-ni":[",", ",", ".", 3], "es-pa":[",", ",", ".", 3], "es-pe":[",", ",", ".", 3], "es-pr":[",", ",", ".", 3], "es-py":[".", ".", ",", 3], "es-sv":[",", ",", ".", 3], "es-uy":[".", ".", ",", 3], "es-ve":[".", ".", ",", 3], "fi-fi":[" ", " ", ",", 3], "fr-be":[".", ".", ",", 3], "fr-ca":[" ", " ", ",", 3], "fr-ch":[" ", " ", ".", 3], "fr-fr":[" ", " ", ",", 3], "fr-lu":[".", ".", ",", 3], "he-il":[",", ",", ".", 3], "hu-hu":[" ", " ", ",", 3], "it-ch":[" ", " ", ".", 3], "it-it":[".", ".", ",", 3], "ja-jp":[",", ",", ".", 3], "ko-kr":[",", ",", ".", 3], "no-no":[".", ".", ",", 3], "nl-be":[" ", " ", ",", 3], "nl-nl":[".", ".", ",", 3], "pl-pl":[".", ".", ",", 3], "pt-br":[".", ".", ",", 3], "pt-pt":[".", ".", "$", 3], "ru-ru":[" ", " ", ",", 3], "sv-se":[".", " ", ",", 3], "tr-tr":[".", ".", ",", 3], "zh-cn":[",", ",", ".", 3], "zh-hk":[",", ",", ".", 3], "zh-tw":[",", ",", ".", 3], "*":[",", ",", ".", 3]};
142
})();
143
dojo.i18n.number._mapToLocalizedFormatData = function (table, locale) {
144
	locale = dojo.hostenv.normalizeLocale(locale);
145
	var data = table[locale];
146
	if (typeof data == "undefined") {
147
		data = table["*"];
148
	}
149
	return data;
150
};
151