Reputation: 27
My project has a Textbox field which is named as txtdisc. I need to minus the entered value from another Textbox called txttotal. The problem is I need to minus the percentage if I enter the percentage sign. For example, if my total value is 1000 and my discount value is 2 then I need the answer as 998 and if the entered value is 2% I need the value as 980. I used _textchanged
event for the calculation.
My code is:
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();
}
}
Upvotes: 1
Views: 681
Reputation: 288
This could be better one:
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();
}
}
Upvotes: 1
Reputation: 994
Try below code:
Use Contains()
method to find if textbox contains % symbol and Use Split()
method to remove % from value:
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();
}
}
Hope it will help you
Upvotes: 0
Reputation: 1856
You can use this code,
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();
}
}
Upvotes: 0
Reputation: 81503
You could look for the percent sign using Contains
If found, you can remove it using Trim('%')
;
Then modify your calculation to do the percentage
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();
}
Resources
String.Contains Method (String)
Upvotes: 4
Reputation: 1722
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;
Here we are just checking whether the string ends with '%'. If yes, we are stripping it off and calculating the percentage of the total.
Upvotes: 0
Reputation: 3424
Try this:
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();
}
}
Explanation:
discount = Convert.ToDecimal(txtdiscount.Text.Replace("%",""));
If you have % in your text just strip it off.
if(txtdiscount.Text.EndsWith("%")
discount = total * (discount/100);
If txtdiscount.Text endswith % then caculate percentage discount, otherwise leave discount as it it.
Important
I suggest you to use decimal.TryParse method instead or Convert.ToDecimal
Upvotes: 2