2012-08-09 68 views
1

我需要將文本字段舍入爲2位小數。這是帶有我設置的警報消息的代碼。我擔心的是當我添加第三個或更多的數字時就會出現問題。 一個例子是100 + 10.11 = 110.11,(結果=)然後110.11 + 10.11 = 120.22然而,當我加上第三個數字120.22 + 10.11時,它等於130.32999999999998,我希望它等於130.33 第三個數字結果「字段等於130.32999999999998Ext JS舍入問題

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;

回答