原文链接:http://blog.csdn.net/lovelyelfpop/article/details/50467084
Sencha touch自带的Number数字输入控件,只有限制数字输入的功能,没有四舍五入和截断指定小数位的功能。
我实现了四舍五入到指定小数位:
代码如下:
Ext.define('UX.field.Number', {
extend: 'Ext.field.Number',
xtype: 'numberfieldex',
config: {
digit: 2 //小数位数, 比如digit为2的时候,3.1415926会四舍五入为3.14
trimZero: true //去除末尾无意义的0,比如3.10会显示为3.1
},
applyValue : function(value){
var minValue = this.getMinValue(),
maxValue = this.getMaxValue();
if (Ext.isNumber(minValue) && Ext.isNumber(value)) {
value = Math.max(value, minValue);
}
if (Ext.isNumber(maxValue) && Ext.isNumber(value)) {
value = Math.min(value, maxValue);
}
value = parseFloat(value).toFixed(this.getDigit());
var display = value.toString();
if (this.getTrimZero()) {
while (display[display.length - 1] == "0")
display = display.substr(0, display.length - 1);
if (display[display.length - 1] == ".")
display = display.substr(0, display.length - 1);
}
return (isNaN(display)) ? '' : parseFloat(display);
},
onBlur: function(e) {
var me = this;
this.callParent(arguments);
var v = this.applyValue(this.getValue());
this.getComponent().input.dom.value = v;
}
});
欢迎加入Sencha Touch + Phonegap交流群
1群:194182999 (满)
2群:419834979
共同学习交流(博主QQ:479858761)