PDA

View Full Version : Why does numberfield ignore decimalPrecision?



borg
9 May 2013, 8:08 AM
Why do I have to override the numberfield to make decimal precision work? Is it a bug? Seems broken to me.


xtype: 'numberfield',
forcePrecision: true, // custom for override, otherwise behaves as default
decimalPrecision: 2


Ext.override(Ext.form.NumberField, {
forcePrecision : false,

valueToRaw: function(value) {
var me = this, decimalSeparator = me.decimalSeparator;
value = me.parseValue(value);
value = me.fixPrecision(value);
value = Ext.isNumber(value) ? value : parseFloat(String(value).replace(decimalSeparator, '.'));
if (isNaN(value)) {
value = '';
}
else {
value = me.forcePrecision ? value.toFixed(me.decimalPrecision) : parseFloat(value);
value = String(value).replace(".", decimalSeparator);
}
return value;
}
});

slemmon
13 May 2013, 8:07 AM
The following code from the 4.2 API examples seems to work ok for me. I enter 23.454 and blur the field and I get 23.45. I enter 23.399 and I get 23.4 since the decimalPrecision defaults to 2. What are you seeing?


Ext.create('Ext.form.Panel', {
title: 'Personal Info',
width: 300,
bodyPadding: 10,
renderTo: Ext.getBody(),
items: [{
xtype: 'numberfield',
anchor: '100%',
name: 'age',
fieldLabel: 'Age',
minValue: 0, //prevents negative numbers


// Remove spinner buttons, and arrow key and mouse wheel listeners
hideTrigger: true,
keyNavEnabled: false,
mouseWheelEnabled: false
}]
});

borg
13 May 2013, 8:18 AM
The issue seems to be that the default behavior of this config param is unexpected. If you have a number like 240.30 it will only show 240.3 If you have 240 it will only show 240. It should be showing 240.30 and 240.00 respectively. I suggest changing the way the config param operates or adding a forcePrecision config option. What do you think?