gpt4 book ai didi

javascript - Ext JS 的舍入问题

转载 作者:行者123 更新时间:2023-11-28 02:45:51 25 4
gpt4 key购买 nike

我需要将文本字段四舍五入到小数点后两位。这是我设置的带有警报消息的代码。我担心的是,当我添加第三个或更多数字时,就会出现问题。一个例子是 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;

最佳答案

您可能会发现 toFixed() 方法很有用。

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number/toFixed

从该页面:

var n = 12345.6789;
n.toFixed(); // Returns "12346": note rounding, no fractional part
n.toFixed(1); // Returns "12345.7": note rounding
n.toFixed(6); // Returns "12345.678900": note added zeros

另请参阅Formatting a number with exactly two decimals in JavaScript .

关于javascript - Ext JS 的舍入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11890627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com