如何计算总金额。 [英] How to calculate total amt.
问题描述
我想计算总数。为此,我使用下面的代码afxt_TextChanged事件。
I want to calculate total. For this I am using following code undertxtDisc_TextChanged event.
qty = Convert.ToInt32(txtQty.Text)
rate = Convert.ToInt32(txtRate.Text)
disc = Convert.ToInt32(txtDisc.Text)
total = qty * rate
amt = total / disc
total1 = total - disc
txtAmt.Text = Convert.ToInt32(total1)
当我放入光盘时,代码计算正确的总数> 0,即如果我使用disc = 0则系统抛出错误:算术运算导致溢出。
另一个问题是如果我第一次把disc = 12& ;在前进之前我将其更改为10然后系统抛出错误:输入字符串格式不正确。
Code calculate correct total when i put disc > 0, i.e If I use disc = 0 then system throws an error :Arithmetic operation resulted in an overflow.
Another problem is If first time I put disc = 12 & before move forward I changed it to 10 then system throws an error : Input string is not in correct format.
推荐答案
在执行计算之前,您必须确保所有输入值都正确。
我建议尝试TryParse尝试Catch块
You must ensure all input values were correct before performing calculation.
I suggest TryParse instead Try Catch block
If Integer.TryParse(txtQty.Text, qty) AndAlso Integer.TryParse(txtRate.Text, rate) AndAlso Integer.TryParse(txtDisc.Text, disc) AndAlso (disc > 0) Then
' Do calculation here
total = qty * rate
amt = total / disc ' I suggest: amt = total \ disc
total1 = total - disc
txtAmt.Text = Convert.ToInt32(total1).ToString()
Else
' Something wrong in input parameter
' Do not calculate, you can give a MessageBox to tell user
End If
因为你是除以零?
尝试:
Because you are dividing by zero?
Try:
If total <> 0 And disc <> 0 Then
amt = total / disc
Else
amt = 0
End If
我不明白你的意思第二个问题,''&在前进之前我将它改为10'' - 你能更详细地解释一下吗?
I don''t get what you mean by the second question, ''& before move forward I changed it to 10 '' - can you explain in more detail?
这篇关于如何计算总金额。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!