我需要将文本字段四舍五入到小数点后两位。这是我设置的带有警报消息的代码。我担心的是,当我添加第三个或更多数字时,就会出现问题。一个例子是 100 + 10.11 = 110.11,(result = ) 然后 110.11 + 10.11 = 120.22 但是,当我添加第三个数字 120.22 + 10.11 时,它等于 130.32999999999998,我希望它等于 130.33 在添加的第三个数字中疯狂的是“结果”字段等于 130.32999999999998
tbar: [{
text: 'Add',
tooltip:'Add the line item',
handler : function(){
var r = new iLineItemRec({
i_line_item_name: '',
i_line_item_amt: ''
});
iLineItemGrid.stopEditing();
iLineItemStore.insert(0, r);
iLineItemGrid.startEditing(0, 0);
Ext.getCmp('mike').setDisabled(false);
},
//Should this be scope:this or scope:iLineItemGrid?
scope:this
},
{
text: 'Delete',
tooltip:'Remove the selected line item',
handler: function(){
iLineItemGrid.stopEditing();
var r = iLineItemGrid.getSelectionModel().getSelectedCell();
iLineItemStore.removeAt(r[0]);
},
//Should this be scope:this or scope:iLineItemGrid?
scope:this
},
{
xtype: 'tbfill'
},
{
id: 'mike',
text: 'Submit',
tooltip:'Submit the line item',
//new
//disabled: true,
handler: function(){
iLineItemGrid.stopEditing();
// Will this code save changes to the database?
//iLineItemGrid.getStore().commitChanges();
iLineItemStore.commitChanges();
var iAmountTotalForLineItems = 0;
var iAmountInDueField = Ext.getCmp('iAmountDue').value;
var tempTotal = 0;
var result = 0;
iLineItemStore.each(function(addAmount){
iAmountTotalForLineItems += addAmount.get('i_line_item_amt');
});
alert('1 iAmountInDueField: ' + iAmountInDueField +' iLineItemTotalHold: '+iLineItemTotalHold + ' iAmountTotalForLineItems: '+ iAmountTotalForLineItems);
if (iLineItemTotalHold > iAmountTotalForLineItems ){
alert ('if');
tempTotal = iLineItemTotalHold - iAmountTotalForLineItems;
result = iAmountInDueField - tempTotal;
alert('two: '+result+' = '+iAmountInDueField+' - '+tempTotal );
}
else if (iLineItemTotalHold < iAmountTotalForLineItems ){
alert ('if2');
tempTotal = iAmountTotalForLineItems - iLineItemTotalHold;
result = iAmountInDueField + tempTotal;
alert('3: '+result+' = '+iAmountInDueField+' + '+tempTotal );
}
iLineItemTotalHold = iAmountTotalForLineItems;
Ext.getCmp('iAmountDue').setValue(result);
this.setDisabled(true);
}
//scope:this
}
]
var iLineItemTotalHold = 0;