Ext JS的舍入问题

时间:2012-08-09 19:30:55

标签: javascript rounding

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

1 个答案:

答案 0 :(得分:1)

您可能会发现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