Subversion Repositories Applications.papyrus

Rev

Rev 1318 | 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
 
11
dojo.provide("dojo.i18n.number");
12
dojo.require("dojo.experimental");
13
dojo.experimental("dojo.i18n.number");
14
dojo.require("dojo.regexp");
15
dojo.require("dojo.i18n.common");
16
dojo.require("dojo.lang.common");
17
dojo.i18n.number.format = function (value, flags, locale) {
18
	flags = (typeof flags == "object") ? flags : {};
19
	var formatData = dojo.i18n.number._mapToLocalizedFormatData(dojo.i18n.number.FORMAT_TABLE, locale);
20
	if (typeof flags.separator == "undefined") {
21
		flags.separator = formatData[1];
22
	}
23
	if (typeof flags.decimal == "undefined") {
24
		flags.decimal = formatData[2];
25
	}
26
	if (typeof flags.groupSize == "undefined") {
27
		flags.groupSize = formatData[3];
28
	}
29
	if (typeof flags.groupSize2 == "undefined") {
30
		flags.groupSize2 = formatData[4];
31
	}
32
	if (typeof flags.round == "undefined") {
33
		flags.round = true;
34
	}
35
	if (typeof flags.signed == "undefined") {
36
		flags.signed = true;
37
	}
38
	var output = (flags.signed && (value < 0)) ? "-" : "";
39
	value = Math.abs(value);
40
	var whole = String((((flags.places > 0) || !flags.round) ? Math.floor : Math.round)(value));
41
	function splitSubstrings(str, count) {
42
		for (var subs = []; str.length >= count; str = str.substr(0, str.length - count)) {
43
			subs.push(str.substr(-count));
44
		}
45
		if (str.length > 0) {
46
			subs.push(str);
47
		}
48
		return subs.reverse();
49
	}
50
	if (flags.groupSize2 && (whole.length > flags.groupSize)) {
51
		var groups = splitSubstrings(whole.substr(0, whole.length - flags.groupSize), flags.groupSize2);
52
		groups.push(whole.substr(-flags.groupSize));
53
		output = output + groups.join(flags.separator);
54
	} else {
55
		if (flags.groupSize) {
56
			output = output + splitSubstrings(whole, flags.groupSize).join(flags.separator);
57
		} else {
58
			output = output + whole;
59
		}
60
	}
61
	if (flags.places > 0) {
62
		var fract = value - Math.floor(value);
63
		fract = (flags.round ? Math.round : Math.floor)(fract * Math.pow(10, flags.places));
64
		output = output + flags.decimal + fract;
65
	}
66
	return output;
67
};
68
dojo.i18n.number.parse = function (value, locale, flags) {
69
	flags = (typeof flags == "object") ? flags : {};
70
	var formatData = dojo.i18n.number._mapToLocalizedFormatData(dojo.i18n.number.FORMAT_TABLE, locale);
71
	if (typeof flags.separator == "undefined") {
72
		flags.separator = formatData[1];
73
	}
74
	if (typeof flags.decimal == "undefined") {
75
		flags.decimal = formatData[2];
76
	}
77
	if (typeof flags.groupSize == "undefined") {
78
		flags.groupSize = formatData[3];
79
	}
80
	if (typeof flags.groupSize2 == "undefined") {
81
		flags.groupSize2 = formatData[4];
82
	}
83
	if (typeof flags.validate == "undefined") {
84
		flags.validate = true;
85
	}
86
	if (flags.validate && !dojo.i18n.number.isReal(value, locale, flags)) {
87
		return Number.NaN;
88
	}
89
	var numbers = value.split(flags.decimal);
90
	if (numbers.length > 2) {
91
		return Number.NaN;
92
	}
93
	var whole = Number(numbers[0].replace(new RegExp("\\" + flags.separator, "g"), ""));
94
	var fract = (numbers.length == 1) ? 0 : Number(numbers[1]) / Math.pow(10, String(numbers[1]).length);
95
	return whole + fract;
96
};
97
dojo.i18n.number.isInteger = function (value, locale, flags) {
98
	flags = (typeof flags == "object") ? flags : {};
99
	var formatData = dojo.i18n.number._mapToLocalizedFormatData(dojo.i18n.number.FORMAT_TABLE, locale);
100
	if (typeof flags.separator == "undefined") {
101
		flags.separator = formatData[1];
102
	} else {
103
		if (dojo.lang.isArray(flags.separator) && flags.separator.length === 0) {
104
			flags.separator = [formatData[1], ""];
105
		}
106
	}
107
	if (typeof flags.groupSize == "undefined") {
108
		flags.groupSize = formatData[3];
109
	}
110
	if (typeof flags.groupSize2 == "undefined") {
111
		flags.groupSize2 = formatData[4];
112
	}
113
	var re = new RegExp("^" + dojo.regexp.integer(flags) + "$");
114
	return re.test(value);
115
};
116
dojo.i18n.number.isReal = function (value, locale, flags) {
117
	flags = (typeof flags == "object") ? flags : {};
118
	var formatData = dojo.i18n.number._mapToLocalizedFormatData(dojo.i18n.number.FORMAT_TABLE, locale);
119
	if (typeof flags.separator == "undefined") {
120
		flags.separator = formatData[1];
121
	} else {
122
		if (dojo.lang.isArray(flags.separator) && flags.separator.length === 0) {
123
			flags.separator = [formatData[1], ""];
124
		}
125
	}
126
	if (typeof flags.decimal == "undefined") {
127
		flags.decimal = formatData[2];
128
	}
129
	if (typeof flags.groupSize == "undefined") {
130
		flags.groupSize = formatData[3];
131
	}
132
	if (typeof flags.groupSize2 == "undefined") {
133
		flags.groupSize2 = formatData[4];
134
	}
135
	var re = new RegExp("^" + dojo.regexp.realNumber(flags) + "$");
136
	return re.test(value);
137
};
138
(function () {
139
	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]};
140
})();
141
dojo.i18n.number._mapToLocalizedFormatData = function (table, locale) {
142
	locale = dojo.hostenv.normalizeLocale(locale);
143
	var data = table[locale];
144
	if (typeof data == "undefined") {
145
		data = table["*"];
146
	}
147
	return data;
148
};
149