计算后将Double转换为Currency

时间:2014-02-13 23:09:57

标签: c# double currency

我已经完成了这段代码,但我不确定它是否可行。但在我尝试执行程序之前,我需要做的最后一件事是转换我创建的双倍计算货币折扣以输出到标签。这是我的代码..

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

3 个答案:

答案 0 :(得分:0)

你的意思是这样的

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

答案 1 :(得分:0)

您应该使用System.Decimal代替System.Double作为货币,还有很多其他条目会详细介绍。这里有一些例子。

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

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

答案 2 :(得分:0)

亲爱的您只需要:

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

它将自动将金额转换为美国标准。例如: 这= 3401402.7至 这个= $3,401,402.70 也将舍入为2精度,如果我们在点后保留1位小数,它将自动在点后返回2点。