1 /* ************************************************************************
3 qooxdoo - the new era of web development
8 2006 by STZ-IDA, Germany, http://www.stz-ida.de
11 LGPL 2.1: http://www.gnu.org/licenses/lgpl.html
14 * Til Schneider (til132)
16 ************************************************************************ */
18 /* ************************************************************************
21 ************************************************************************ */
24 * A formatter and parser for numbers.
26 qx.OO.defineClass("qx.util.format.NumberFormat", qx.util.format.Format,
28 qx.util.format.Format.call(this);
33 * The minimum number of integer digits (digits before the decimal separator).
34 * Missing digits will be filled up with 0 ("19" -> "0019").
36 qx.OO.addProperty({ name:"minimumIntegerDigits", type:"number", defaultValue:0, allowNull:false });
39 * The maximum number of integer digits (superfluos digits will be cut off
42 qx.OO.addProperty({ name:"maximumIntegerDigits", type:"number", defaultValue:null });
45 * The minimum number of fraction digits (digits after the decimal separator).
46 * Missing digits will be filled up with 0 ("1.5" -> "1.500")
48 qx.OO.addProperty({ name:"minimumFractionDigits", type:"number", defaultValue:0, allowNull:false });
51 * The maximum number of fraction digits (digits after the decimal separator).
52 * Superflous digits will cause rounding ("1.8277" -> "1.83")
54 qx.OO.addProperty({ name:"maximumFractionDigits", type:"number", defaultValue:null });
56 /** Whether thousand groupings should be used {e.g. "1,432,234.65"}. */
57 qx.OO.addProperty({ name:"groupingUsed", type:"boolean", defaultValue:true, allowNull:false });
59 /** The prefix to put before the number {"EUR " -> "EUR 12.31"}. */
60 qx.OO.addProperty({ name:"prefix", type:"string", defaultValue:"", allowNull:false });
62 /** Sets the postfix to put after the number {" %" -> "56.13 %"}. */
63 qx.OO.addProperty({ name:"postfix", type:"string", defaultValue:"", allowNull:false });
69 * @param num {number} the number to format.
70 * @return {string} the formatted number as a string.
72 qx.Proto.format = function(num) {
73 var NumberFormat = qx.util.format.NumberFormat;
75 var negative = (num < 0);
79 if (this.getMaximumFractionDigits() != null) {
81 var mover = Math.pow(10, this.getMaximumFractionDigits());
82 num = Math.round(num * mover) / mover;
85 if (num != 0) { // Math.log(0) = -Infinity
86 var integerDigits = Math.max(parseInt(Math.log(num) / Math.LN10) + 1, 1);
91 var numStr = "" + num;
93 // Prepare the integer part
94 var integerStr = numStr.substring(0, integerDigits);
95 while (integerStr.length < this.getMinimumIntegerDigits()) {
96 integerStr = "0" + integerStr;
98 if (this.getMaximumIntegerDigits() != null && integerStr.length > this.getMaximumIntegerDigits()) {
99 // NOTE: We cut off even though we did rounding before, because there
100 // may be rounding errors ("12.24000000000001" -> "12.24")
101 integerStr = integerStr.substring(integerStr.length - this.getMaximumIntegerDigits());
104 // Prepare the fraction part
105 var fractionStr = numStr.substring(integerDigits + 1);
106 while (fractionStr.length < this.getMinimumFractionDigits()) {
109 if (this.getMaximumFractionDigits() != -1 && fractionStr.length > this.getMaximumFractionDigits()) {
110 // We have already rounded -> Just cut off the rest
111 fractionStr = fractionStr.substring(0, this.getMaximumFractionDigits());
114 // Add the thousand groupings
115 if (this.getGroupingUsed()) {
116 var origIntegerStr = integerStr;
119 for (groupPos = origIntegerStr.length; groupPos > 3; groupPos -= 3) {
120 integerStr = NumberFormat.GROUPING_SEPARATOR
121 + origIntegerStr.substring(groupPos - 3, groupPos) + integerStr;
123 integerStr = origIntegerStr.substring(0, groupPos) + integerStr;
126 // Workaround: prefix and postfix are null even their defaultValue is "" and
127 // allowNull is set to false?!?
128 var prefix = this.getPrefix() ? this.getPrefix() : "";
129 var postfix = this.getPostfix() ? this.getPostfix() : "";
131 // Assemble the number
132 var str = prefix + (negative ? "-" : "") + integerStr;
133 if (fractionStr.length > 0) {
134 str += NumberFormat.DECIMAL_SEPARATOR + fractionStr;
145 * @param str {string} the string to parse.
147 * @return {double} the number.
149 qx.Proto.parse = function(str) {
150 var NumberFormat = qx.util.format.NumberFormat;
152 // use the escaped separators for regexp
153 var groupSepEsc = qx.lang.String.escapeRegexpChars(NumberFormat.GROUPING_SEPARATOR);
154 var decimalSepEsc = qx.lang.String.escapeRegexpChars(NumberFormat.DECIMAL_SEPARATOR);
156 var regex = new RegExp(qx.lang.String.escapeRegexpChars(this.getPrefix())
157 + '(-)?([0-9' + groupSepEsc + ']+)'
158 + '(' + decimalSepEsc + '\\d+)?'
159 + qx.lang.String.escapeRegexpChars(this.getPostfix()));
161 var hit = regex.exec(str);
163 throw new Error("Number string '" + str + "' does not match the number format");
166 var negative = (hit[1] == "-");
167 var integerStr = hit[2];
168 var fractionStr = hit[3];
170 // Remove the thousand groupings
171 integerStr = integerStr.replace(new RegExp(groupSepEsc), "");
173 var asStr = (negative ? "-" : "") + integerStr;
174 if (fractionStr != null && fractionStr.length != 0) {
175 // Remove the leading decimal separator from the fractions string
176 fractionStr = fractionStr.replace(new RegExp(decimalSepEsc),"");
177 asStr += "." + fractionStr;
179 return parseFloat(asStr);
184 * Returns the default number format.
186 * @return {NumberFormat} the default number format.
188 qx.Class.getInstance = function() {
189 var NumberFormat = qx.util.format.NumberFormat;
190 if (NumberFormat._instance == null) {
191 NumberFormat._instance = new NumberFormat();
193 return NumberFormat._instance;
198 * Returns an integer number format.
200 * @return {NumberFormat} an integer number format.
202 qx.Class.getIntegerInstance = function() {
203 var NumberFormat = qx.util.format.NumberFormat;
204 if (NumberFormat._integerInstance == null) {
205 NumberFormat._integerInstance = new NumberFormat();
206 NumberFormat._integerInstance.setMaximumFractionDigits(0);
208 return NumberFormat._integerInstance;
212 /** {string} The decimal separator. */
213 qx.Class.DECIMAL_SEPARATOR = ".";
215 /** {string} The thousand grouping separator. */
216 qx.Class.GROUPING_SEPARATOR = ",";