PDA

View Full Version : Number field currency format format problem



sahinci
2 Sep 2011, 12:07 AM
Hi all,
I'm using numberfield on my accounting project. But i couldnt correct number fields format on my project. For example if number is 1,01 its showing normal. But if number is 1,00 just showing number 1 but should to show 1,00 . I couldnt adjust with ext.js. I investigate on this form found some example tried but didnt work. If somebody has exact answer please share with.

Sincerely

skirtle
3 Sep 2011, 1:17 PM
Here's something that might work. You may need to tweak it to get the locale working the way you want. The usual warnings about not using floating point numbers to represent monetary values apply.


Ext.define('CurrencyField', {
extend: 'Ext.form.field.Number',

// Some sensible defaults
allowDecimals: true,
hideTrigger: true,
keyNavEnabled: false,
mouseWheelEnabled: false,

valueToRaw: function() {
var value = this.parseValue(this.callParent(arguments));

return Ext.util.Format.number(value, '0.00');
}
});

Ext.create('CurrencyField', {
renderTo: Ext.getBody()
});

sahinci
12 Sep 2011, 10:54 PM
Hi skirtle ,
Thank you very much for your answer. I investigate very much about that problem and finally i decided(learned) to override my numberfield. That following codes showing how to override number field. Its working nl and Gb culture formats it has dynamic group and decimal seperators. so doesnt matter which culture u are using its working correct.
Everyone can try if they are using money calculations in their projects.


<script language="javascript" type="text/javascript">
var originalBlur = Ext.form.NumberField.prototype.onBlur;
Ext.override(Ext.form.NumberField, {
baseChars: "0123456789" + '<%=System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator%>',
floatValue: 0.0,
setValue: function (v) {
this.decimalPrecision = 2;
this.decimalSeparator = '<%=System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator%>';
this.thousandSeparator = '<%=System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyGroupSeparator %>';
this.floatValue = v;
v = Ax.MoneyFormat(false, v, this.thousandSeparator, this.decimalSeparator, '', false);

this.setRawValue(v);
this.fireEvent('AfterRender', this, v, v);
return Ext.form.NumberField.superclass.setValue.call(this, v);
}
,
validateValue: function (value) {
return true;
}
,
parseValue: function (value) {
if (this.decimalSeparator == ",") {
value = parseFloat(String(value).replace(this.decimalSeparator, ",").replace(/,/g, ""));
}
else {
value = parseFloat(String(value).replace(this.decimalSeparator, ".").replace(/,/g, ""));
}
return isNaN(value) ? '' : value;
},
getRawValue: function () {
return this.floatValue;
}
,
getValue: function () {
return this.floatValue;
},
getSubmitValue: function () {
return String(this.floatValue).replace('.', this.decimalSeparator);
}
,
beforeBlur: function () {
var v = this.parseValue(this.getRawValue());
if (!Ext.isEmpty(v)) {
this.setValue(v);
}
}
,
onBlur: function (field) {
var value = Ext.form.NumberField.superclass.getValue.call(this);
value = value.replace(this.thousandSeparator, '');
value = value.replace(this.decimalSeparator, '.');
this.setValue(parseFloat(value));
originalBlur.apply(this, arguments);
this.fireEvent('Change', this, parseFloat(value), parseFloat(value));
}
,
initEvents: function () {
var allowed = this.baseChars + '';
if (this.allowDecimals) {
allowed += this.decimalSeparator;
}
if (this.allowNegative) {
allowed += '-';
}
allowed = Ext.escapeRe(allowed);
this.maskRe = new RegExp('[' + allowed + ']');
if (this.autoStripChars) {
this.stripCharsRe = new RegExp('[^' + allowed + ']', 'gi');
}

Ext.form.NumberField.superclass.initEvents.call(this);
}
});

</script>