1

I have all but finished this code but I am not sure it will work yet. But the last thing I need before i try to execute the program to work is to convert a double I created to calculate discounts to currency to output to a label. Here is my code..

   private void btnCharacteristicsContinue_Click(object sender, EventArgs e)
    {

        int intStudentClass = lstClass.SelectedIndex;
        int intParkingSpace = lstNeedsCampusParking.SelectedIndex;
        double dblParkingDiscount;
        double dblFinalParking;
        if (lstNeedsCampusParking.SelectedIndex.ToString() == ("No"))
        {
            dblFinalParking = 0;
        }
        else
        {
            switch (intStudentClass)
            {
                case 0:
                case 1:
                    dblParkingDiscount = 300;
                    if (lstLiveOnCampus.SelectedIndex.ToString() == ("Yes"))
                    {
                        dblFinalParking = dblParkingDiscount * .25;
                    }
                    else
                    {
                        dblFinalParking = dblParkingDiscount;
                    }
                    break;

                case 2:
                case 3:
                    dblParkingDiscount = 300 * .1;
                    if (lstLiveOnCampus.SelectedIndex.ToString() == ("Yes"))
                    {
                        dblFinalParking = dblParkingDiscount * .25;
                    }
                    else
                    {
                        dblFinalParking = dblParkingDiscount;
                    }
                    break;
                case 4:
                    dblParkingDiscount = 300 * .2;
                    if (lstLiveOnCampus.SelectedIndex.ToString() == ("Yes"))
                    {
                        dblFinalParking = dblParkingDiscount * .25;
                    }
                    else
                    {
                        dblFinalParking = dblParkingDiscount;
                    }
                    break;
            }
            lblCharacteristicsOutput.Text = dblFinalParking;      
        }
  • http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#CFormatString – Tim Schmelter Feb 13 '14 at 23:13
  • Just a note: You can remove braces around string literals. `"Yes"` instead of `("Yes")` – Anri Feb 13 '14 at 23:13
  • 2
    I'd *strongly* recommend against using double here to start with. Decimal is a much more appropriate type for currency values. – Jon Skeet Feb 13 '14 at 23:28
  • You should avoid hungarian notation (http://stackoverflow.com/questions/111933/why-shouldnt-i-use-hungarian-notation) - you're probably going to change your double type to decimal - are you going to rename all your variables too? – Craig Shearer Feb 13 '14 at 23:52

3 Answers3

1

dear you just need to :

lblCharacteristicsOutput.Text = dblFinalParking.ToString("C2");

it will automatically convert amount to US Standard. for example: this = 3401402.7 to this = $3,401,402.70 also it will round to 2 precision and if we have 1 decimal after point it will automatically return 2 points after point.

0

you mean like this

lblCharacteristicsOutput.Text = dblFinalParking.ToString("C0");  
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
0

You should use System.Decimal instead of System.Double for currency, there are quite a few other entries that go into more details. Here are a few examples.

In .net, how do I choose between a Decimal and a Double

decimal vs double! - Which one should I use and when?

Community
  • 1
  • 1
Josh Anderson
  • 438
  • 3
  • 7