我的项目有一个 Textbox 字段,名为 txtdisc 。我需要从另一个名为 txttotal 的 Textbox 中减去输入的值。问题是,如果输入百分号,我需要减去百分比。例如,如果我的总值是1000而我的折扣值是2,那么我需要答案为998,如果输入的值是2%,我需要将值作为980.我使用_textchanged
事件进行计算。 / p>
我的代码是:
private void txtdiscount_TextChanged(object sender, EventArgs e)
{
if (txtdiscount.Text.Length > 0 && lbltotal.Text != "")
{
decimal net = 0, total = 0, discount = 0;
total = Convert.ToDecimal(lbltotal.Text);
discount = Convert.ToDecimal(txtdiscount.Text);
net =total- discount;
lblnetamount.Text = net.ToString();
}
}
答案 0 :(得分:4)
您可以使用Contains
如果找到,您可以使用Trim('%')
;
然后修改您的计算以执行百分比
if (txtdiscount.Text.Length > 0 && lbltotal.Text != "")
{
decimal net = 0, total = 0, discount = 0;
total = Convert.ToDecimal(lbltotal.Text);
if (txtdiscount.Contains("%"))
{
discount = Convert.ToDecimal(txtdiscount.Text.Trim('%'));
net = total - (total * (discount / 100));
}
else
{
discount = Convert.ToDecimal(txtdiscount.Text);
net = total - discount;
}
lblnetamount.Text = net.ToString();
}
<强>资源强>
答案 1 :(得分:2)
试试这个:
private void txtdiscount_TextChanged(object sender, EventArgs e)
{
if (txtdiscount.Text.Length > 0 && lbltotal.Text != "")
{
decimal net = 0, total = 0, discount = 0;
total = Convert.ToDecimal(lbltotal.Text);
discount = Convert.ToDecimal(txtdiscount.Text.Replace("%",""));
if(txtdiscount.Text.EndsWith("%")
discount = total * (discount/100);
net = total- discount;
lblnetamount.Text = net.ToString();
}
}
说明:
discount = Convert.ToDecimal(txtdiscount.Text.Replace("%",""));
如果您的文字中有%,则将其剥离。
if(txtdiscount.Text.EndsWith("%")
discount = total * (discount/100);
如果txtdiscount.Text以%结尾,则计算百分比折扣,否则保留折扣。
重要强>
我建议您使用decimal.TryParse方法或Convert.ToDecimal
答案 2 :(得分:1)
这可能是更好的一个:
private void txtdiscount_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtdiscount.Text) && !string.IsNullOrEmpty(lbltotal.Text))
{
decimal net = 0, total = 0, discount = 0;
total = decimal.TryParse(lbltotal.Text, out total);
discount = decimal.TryParse(txtdiscount.Text.Replace("%",""), out discount);
discount = txtdiscount.Text.EndsWith("%")?total * (discount/100):discount;
net = total- discount;
lblnetamount.Text = net.ToString();
}
}
答案 3 :(得分:0)
int discount, total = 1000, net;
if(txtdiscount.EndsWith("%"))
discount = total*Convert.ToInt32(txtdiscount.Substring(0, st.Length-1))/100;
else
discount =Convert.ToInt32(txtdiscount.Substring(0, st.Length-1));
net = total -discount;
这里我们只是检查字符串是否以'%'结尾。如果是,我们将剥离它并计算总数的百分比。
答案 4 :(得分:0)
尝试以下代码:
使用Contains()
方法查找文本框是否包含%符号,并使用Split()
方法从值中删除%:
private void txtdiscount_TextChanged(object sender, EventArgs e)
{
if (txtdiscount.Text.Length > 0 && lbltotal.Text != "")
{
decimal net = 0, total = 0, discount = 0;
total = Convert.ToDecimal(lbltotal.Text);
if(txtdiscount.Text.Contains('%'))
{
discount = Convert.ToDecimal(txtdiscount.Text.split('%')[0]);
net = total - total*discount/100;
}
else
{
discount = Convert.ToDecimal(txtdiscount.Text);
net =total- discount;
}
lblnetamount.Text = net.ToString();
}
}
希望它能帮到你
答案 5 :(得分:0)
您可以使用此代码
private void txtdiscount_TextChanged(object sender, EventArgs e)
{
if (txtdiscount.Text.Length > 0 && lbltotal.Text != "")
{
decimal net = 0, total = 0, discount = 0;
total = Convert.ToDecimal(lbltotal.Text);
if (txtdiscount.Text.IndexOf('%') != -1)
{
discount = total * Convert.ToDecimal(txtdiscount.Text.Split('%')[0])/100;
}
else
{
discount = Convert.ToDecimal(txtdiscount.Text);
}
net =total- discount;
lblnetamount.Text = net.ToString();
}
}