Question

I'm new to stackoverflow and programming for that matter. I have the following code:

      if (age <= 55)
       {                      
           discount = 0.00;
           optDouglas.Checked = true;
       }
       if (age > 55)
       {
           discount = 0.2 * monthlyfee;     
           optSenior.Checked = true;                    
       }

lstInfo.Items.Add(string.Format(formatline1, "Program Discount", discount.ToString("C2")));

I want to show the discount in parentheses in the ListBox when there is a discount and without parentheses when discount is zero. Thanks in advance.

Was it helpful?

Solution

This is another way to do it:

var formatDiscount = discount != 0 ? "({0})" : "{0}";
lstInfo.Items.Add(string.Format(formatline1, "Program Discount", string.Format(formatDiscount, discount.ToString("C2"))));

OTHER TIPS

Not the best way to do it, but I think you want something like this:

lstInfo.Items.Add(string.Format(formatline1, "Program Discount", discount > 0 ? "(" + discount.ToString("C2") + ")" : discount.ToString("C2")));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top