PDA

View Full Version : Ext.util.Format.formatNumber



Condor
29 Sep 2008, 11:29 PM
Ext.util.Format has a usMoney function, but that one is unusable for other locales.

IMHO Ext is missing a configurable formatNumber function:

Ext.apply(Ext.util.Format, {
numberFormat: {
decimalSeparator: '.',
decimalPrecision: 2,
groupingSeparator: ',',
groupingSize: 3,
currencySymbol: '$'
},
formatNumber: function(value, numberFormat) {
var format = Ext.apply(Ext.apply({}, Ext.util.Format.numberFormat), numberFormat);
if (typeof value !== 'number') {
value = String(value);
if (format.currencySymbol) {
value = value.replace(format.currencySymbol, '');
}
if (format.groupingSeparator) {
value = value.replace(new RegExp(format.groupingSeparator, 'g'), '');
}
if (format.decimalSeparator !== '.') {
value = value.replace(format.decimalSeparator, '.');
}
value = parseFloat(value);
}
var neg = value < 0;
value = Math.abs(value).toFixed(format.decimalPrecision);
var i = value.indexOf('.');
if (i >= 0) {
if (format.decimalSeparator !== '.') {
value = value.slice(0, i) + format.decimalSeparator + value.slice(i + 1);
}
} else {
i = value.length;
}
if (format.groupingSeparator) {
while (i > format.groupingSize) {
i -= format.groupingSize;
value = value.slice(0, i) + format.groupingSeparator + value.slice(i);
}
}
if (format.currencySymbol) {
value = format.currencySymbol + value;
}
if (neg) {
value = '-' + value;
}
return value;
}
});

Animal
30 Sep 2008, 12:29 AM
The 3.0 code branch has an Ext.util.Format.number() function with the following documentation comment:



/**
* Formats the number according to the format string; adheres to the american number standard
* where a comma is inserted after every 3 digits. note: there should be only 1 contiguous number
* in the format, where a number consists of digits, period, and commas
* any other characters can be wrapped around this number, including ?$?, ?%?, or text
* <div style="margin-left:40px">examples (123456.789):
* <div style="margin-left:10px">
* ?0? - (123456) show only digits, no precision<br>
* ?0.00? - (123456.78) show only digits, 2 precision<br>
* ?0.0000? - (123456.7890) show only digits, 4 precision<br>
* ?0,000? - (123,456) show comma and digits, no precision<br>
* ?0,000.00? - (123,456.78) show comma and digits, 2 precision<br>
* ?0,0.00? - (123,456.78) shortcut method, show comma and digits, 2 precision<br>
* </div</div>
*
* @method format
* @param v {Number} The number to format.
* @param format {String} The way you would like to format this text.
* @return {String} The formatted number.
* @public
*/

Animal
30 Sep 2008, 12:31 AM
Yours does look more flexible though. Perhaps you and Jack can come up with some cmobination that provides the extra features you add.

jerrybrown5
30 Sep 2008, 8:51 PM
Tks Condor. This issue just came up on my bug list.

mjlecomte
1 Oct 2008, 5:45 AM
+4, 4 because I've seen as many other threads in just the last week which would utilize this.

S1-Gezi
10 Dec 2008, 7:24 AM
Do I have a chance to see the source code of Ext.util.Format.number() today?
How can I (read-only) access the Ext 3.0 branch?

Thanks Frank


The 3.0 code branch has an Ext.util.Format.number() function with the following documentation comment:



/**
* Formats the number according to the format string; adheres to the american number standard
* where a comma is inserted after every 3 digits. note: there should be only 1 contiguous number
* in the format, where a number consists of digits, period, and commas
* any other characters can be wrapped around this number, including ?$?, ?%?, or text
* <div style="margin-left:40px">examples (123456.789):
* <div style="margin-left:10px">
* ?0? - (123456) show only digits, no precision<br>
* ?0.00? - (123456.78) show only digits, 2 precision<br>
* ?0.0000? - (123456.7890) show only digits, 4 precision<br>
* ?0,000? - (123,456) show comma and digits, no precision<br>
* ?0,000.00? - (123,456.78) show comma and digits, 2 precision<br>
* ?0,0.00? - (123,456.78) shortcut method, show comma and digits, 2 precision<br>
* </div</div>
*
* @method format
* @param v {Number} The number to format.
* @param format {String} The way you would like to format this text.
* @return {String} The formatted number.
* @public
*/

Condor
10 Dec 2008, 7:53 AM
You are a premium member, so you should have a username/password for:
http://code.extjs.com/svn/ext/trunk/src/util/Format.js