我试图用数量的总和来计算 totalAmount 的折扣 和 unitPrice 。折扣可以是可选的。可以使用或不使用它取决于用户是否要在折扣文本框中输入一些值。
如果我不打算在其上输入值,我所做的就是将折扣文本框保留为空 但我的 totalAmount 仍然为输出提供零值。
protected void btnCalculate_Click(object sender, EventArgs e)
{
double quantity, unitPrice,totalAmount;
double discount;
double d;
bool canProcess = true;
//Quantity
if (!double.TryParse(lblQuantity.Text, out quantity))
{
//Conversion Failed
string script = "alert(\"Quantity cannot be converted\");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);
}
//UnitPrice
if (!double.TryParse(txtUnitPrice.Text, out unitPrice))
{
//Conversion Failed
string script = "alert(\"Unit Price cannot be converted\");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);
}
if (!double.TryParse(txtDisc.Text, out discount))
{
//Conversion Failed
string script = "alert(\"discount cannot be converted\");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript",script,true);
}
if (canProcess)
{
d = quantity * unitPrice;
totalAmount = ((d*discount)/100);
lblTotalAmount.Text = totalAmount.ToString();
}
}
//#calculator end
}
答案 0 :(得分:1)
如果您希望折扣是可选的,请确定您希望如何计算金额:
if (canProcess)
{
d = quantity * unitPrice;
if(discount == 0)
{
totalAmount = d;// any calculation
}
else
{
totalAmount = ((d*discount)/100);
}
lblTotalAmount.Text = totalAmount.ToString();
}
答案 1 :(得分:0)
当然你需要检查第一个折扣大于0,然后你可以计算总金额
if (canProcess)
{
d = quantity * unitPrice;
if(discount == 0)
{
totalAmount = d;
}
else
{
totalAmount = ((d*discount)/100);// if here d is 0 your total amt will be 0.
}
lblTotalAmount.Text = totalAmount.ToString();
}