简体   繁体   中英

Calculate percentage if percentage sign is in the text

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();
  }
}

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.Trim Method (Char[])

String.Contains Method (String)

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

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();
    }
}
        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.

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

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();

    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM