pkli
23 Jun 2011, 4:17 AM
Hi,
Here is two (very) simple modifications for "numberfield" prototype.
For beginners ...
A new param "forceDecimals", to force formatting of the string value with as many decimals than "decimalPrecision".
Eg :
decimalPrecision : 2
forceDecimals : true
value : 125.2
=> display/submit value : 125.20
I'm not a javascript expert ...
Comments, criticals or suggestions are welcome ...
bonus track ... As french user, I use "," (comma) as decimalSeparator. A simple way to use "." (dot) as secondary allowed decimalSeparator is to allow it in baseChars ...
So you can use any of them (useful to allow copy/paste from others software using dot as decimal separator)...
if (Ext.form.field.Number){
Ext.apply(Ext.form.field.Number.prototype, {
// force decimals string value
forceDecimals : true,
valueToRaw: function(value) {
var me = this,decimalSeparator = me.decimalSeparator,precision = me.decimalPrecision,forceDecimals = me.forceDecimals;
value = me.parseValue(value);
value = me.fixPrecision(value);
value = Ext.isNumber(value) ? value : parseFloat(String(value).replace(decimalSeparator, '.'));
if (isNaN(value)) return '';
else {
value = String(value).replace('.', decimalSeparator);
if (value.indexOf(decimalSeparator)>=0 && forceDecimals) {
var dec = value.substr(value.indexOf(decimalSeparator)+1);
for(var i=dec.length;i<precision;i++) {
value = value+"0";
}
}
return value;
}
},
// allow dot as decimalSeparator even if this is not the locale setting
baseChars : '0123456789.',
});
}
Here is two (very) simple modifications for "numberfield" prototype.
For beginners ...
A new param "forceDecimals", to force formatting of the string value with as many decimals than "decimalPrecision".
Eg :
decimalPrecision : 2
forceDecimals : true
value : 125.2
=> display/submit value : 125.20
I'm not a javascript expert ...
Comments, criticals or suggestions are welcome ...
bonus track ... As french user, I use "," (comma) as decimalSeparator. A simple way to use "." (dot) as secondary allowed decimalSeparator is to allow it in baseChars ...
So you can use any of them (useful to allow copy/paste from others software using dot as decimal separator)...
if (Ext.form.field.Number){
Ext.apply(Ext.form.field.Number.prototype, {
// force decimals string value
forceDecimals : true,
valueToRaw: function(value) {
var me = this,decimalSeparator = me.decimalSeparator,precision = me.decimalPrecision,forceDecimals = me.forceDecimals;
value = me.parseValue(value);
value = me.fixPrecision(value);
value = Ext.isNumber(value) ? value : parseFloat(String(value).replace(decimalSeparator, '.'));
if (isNaN(value)) return '';
else {
value = String(value).replace('.', decimalSeparator);
if (value.indexOf(decimalSeparator)>=0 && forceDecimals) {
var dec = value.substr(value.indexOf(decimalSeparator)+1);
for(var i=dec.length;i<precision;i++) {
value = value+"0";
}
}
return value;
}
},
// allow dot as decimalSeparator even if this is not the locale setting
baseChars : '0123456789.',
});
}