C# 如果文本中有百分号,则计算百分比
我的项目有一个名为txtdisc的文本框字段。我需要从另一个名为txttotal的文本框中减去输入的值。问题是如果我输入百分号,我需要减去百分比。例如,如果我的总值为1000,折扣值为2,则我需要答案为998,如果输入的值为2%,则我需要值为980。我使用C# 如果文本中有百分号,则计算百分比,c#,C#,我的项目有一个名为txtdisc的文本框字段。我需要从另一个名为txttotal的文本框中减去输入的值。问题是如果我输入百分号,我需要减去百分比。例如,如果我的总值为1000,折扣值为2,则我需要答案为998,如果输入的值为2%,则我需要值为980。我使用\u textchanged事件进行计算 我的代码是: private void txtdiscount_TextChanged(object sender, EventArgs e) { if (txtdiscount.Text.Leng
\u textchanged
事件进行计算
我的代码是:
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();
}
}
试试这个:
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();
}
}
说明:
如果你的文本中有%,只需将其去掉即可
if(txtdiscount.Text.EndsWith("%")
discount = total * (discount/100);
如果txtdiscount.Text以%结尾,则计算百分比折扣,否则保留折扣
重要
我建议您改用decimal.TryParse方法或Convert.ToDecimal尝试以下方法:
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();
}
}
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;
说明:
如果你的文本中有%,只需将其去掉即可
if(txtdiscount.Text.EndsWith("%")
discount = total * (discount/100);
如果txtdiscount.Text以%结尾,则计算百分比折扣,否则保留折扣
重要
我建议您改用decimal.TryParse方法或Convert.ToDecimal方法
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;
这里我们只是检查字符串是否以“%”结尾。如果是的话,我们将其剥离,并计算其占总数的百分比
这里我们只是检查字符串是否以“%”结尾。如果是,我们将其剥离并计算总数的百分比。您可以使用
Contains
如果找到,可以使用修剪('%')将其删除。
然后修改计算以进行百分比计算
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();
}
资源
您可以使用
Contains
如果找到,可以使用修剪('%')将其删除。
然后修改计算以进行百分比计算
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();
}
资源
请尝试以下代码:
使用Contains()
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();
}
}
希望它能帮助您尝试以下代码:
使用Contains()
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();
}
}
希望它能帮助您您可以使用此代码
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();
}
}
你可以使用这个代码
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();
}
}
这可能是一个更好的方法:
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();
}
}
这可能是一个更好的方法:
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();
}
}
您应该始终在答案中添加信息以了解代码的作用。您应该始终在答案中添加信息以了解代码的作用。