Yes, well, at the end I decided to backport the ExtJS code, I have license for both components, anyway the ExtJS code is free and you can check the source code in the docs, so here is the function for anyone that might need it:
Consider that this is inside an object called myObject, you will have to change it to your own thing in case you want to use this, also the defaults thousandSeparator, decimalSeparator and numberFormat. This is just an example.
Code:
myObject :
{
thousandSeparator : '.',
decimalSeparator : ',',
numberFormat : '0.000,00/i',
number : function(v, formatString)
{
if (!formatString)
{
return v;
}
v = Ext.Number.from(v, NaN);
if (isNaN(v)) {
return '';
}
var comma = myObject.thousandSeparator,
dec = myObject.decimalSeparator,
i18n = false,
neg = v < 0,
hasComma,
psplit,
fnum,
cnum,
parr,
j,
m,
n,
i;
v = Math.abs(v);
// The "/i" suffix allows caller to use a locale-specific formatting string.
// Clean the format string by removing all but numerals and the decimal separator.
// Then split the format string into pre and post decimal segments according to *what* the
// decimal separator is. If they are specifying "/i", they are using the local convention in the format string.
if (formatString.substr(formatString.length - 2) == '/i')
{
var myI18NFormatCleanRe = I18NFormatCleanRe;
if (!myI18NFormatCleanRe)
{
myI18NFormatCleanRe = new RegExp('[^\\d\\' + myObject.decimalSeparator + ']','g');
}
formatString = formatString.substr(0, formatString.length - 2);
i18n = true;
hasComma = formatString.indexOf(comma) != -1;
psplit = formatString.replace(myI18NFormatCleanRe, '').split(dec);
}
else
{
hasComma = formatString.indexOf(',') != -1;
psplit = formatString.replace(formatCleanRe, '').split('.');
}
if (psplit.length > 2)
{
//<debug>
Ext.Error.raise(
{
sourceClass: "Ext.util.Format",
sourceMethod: "number",
value: v,
formatString: formatString,
msg: "Invalid number format, should have no more than 1 decimal"
});
//</debug>
}
else if (psplit.length > 1)
{
v = Ext.Number.toFixed(v, psplit[1].length);
}
else
{
v = Ext.Number.toFixed(v, 0);
}
fnum = v.toString();
psplit = fnum.split('.');
if (hasComma)
{
cnum = psplit[0];
parr = [];
j = cnum.length;
m = Math.floor(j / 3);
n = cnum.length % 3 || 3;
for (i = 0; i < j; i += n)
{
if (i !== 0) {
n = 3;
}
parr[parr.length] = cnum.substr(i, n);
m -= 1;
}
fnum = parr.join(comma);
if (psplit[1])
{
fnum += dec + psplit[1];
}
}
else
{
if (psplit[1])
{
fnum = psplit[0] + dec + psplit[1];
}
}
if (neg)
{
/*
* Edge case. If we have a very small negative number it will get rounded to 0,
* however the initial check at the top will still report as negative. Replace
* everything but 1-9 and check if the string is empty to determine a 0 value.
*/
neg = fnum.replace(/[^1-9]/g, '') !== '';
}
return (neg ? '-' : '') + formatString.replace(/[\d,?\.?]+/, fnum);
}
}
You can see the original code for this function here: http://docs.sencha.com/ext-js/4-1/so...-method-number
Enjoy it!
BTW, for Sencha guys, is there a good reason not to have included this function in the official library?