【问题标题】:Number won't go to two decimal places数字不会保留两位小数
【发布时间】:2014-02-19 23:12:48
【问题描述】:

我正在申请兴趣。我试图将总金额的利息金额精确到小数点后 2 位。我在代码中一直使用("N2"),除了标签中的一个区域外,它一直有效。

我使用标签在Radiobutton() 区域显示总金额,它仍然显示完整的数字eg.14.8282423432,而不是小数点后两位。有什么办法可以解决这个问题?谢谢。

public void ShowRates()
    {
        Amount = ((Form1)Owner).Amount;
        WeekInterestRate = ((Form1)Owner).WeekInterestRate;
        TwoWeekInterestRate = ((Form1)Owner).TwoWeekInterestRate;
        MonthInterestRate = ((Form1)Owner).MonthInterestRate;
        ThreeMonthInterestRate = ((Form1)Owner).ThreeMonthInterestRate;

        WeekRateLabel.Text = WeekInterestRate.ToString("N2");
        TwoWeekLabel.Text = TwoWeekInterestRate.ToString("N2");
        MonthRateLabel.Text = MonthInterestRate.ToString("N2");
        TMonthRateLabel.Text = ThreeMonthInterestRate.ToString("N2");

        WeekPercent = (Amount * WeekInterestRate / 100);
        WeekInterestAmount = ((WeekPercent / 365) * 7);
        label6.Text = WeekInterestAmount.ToString("N2");

        TwoWeekInterestPercent = (Amount * TwoWeekInterestRate / 100);
        TwoWeekInterestAmount = ((TwoWeekInterestPercent / 365) * 14);
        label7.Text = TwoWeekInterestAmount.ToString("N2");

        MonthPercent = (Amount * MonthInterestRate / 100);
        MonthInterestAmount = ((MonthPercent / 365) * 30);
        label8.Text = MonthInterestAmount.ToString("N2");

        ThreeMonthPercent = (Amount * ThreeMonthInterestRate / 100);
        ThreeMonthInterestAmount = ((ThreeMonthPercent / 365) * 90);
        label9.Text = ThreeMonthInterestAmount.ToString("N2");
    }


    public void Back()
    {
        Form1 f1 = new Form1();
        f1.Show(this);
        Hide();
    }
    public void RadioButtons()
    {

        if (radioButton2.Checked == true)
        {
            FinalAmount = (Amount + WeekInterestAmount);
            FinalAmount.ToString("N2");
            label10.Text = "Total Amount After 7 Days" + " " + "€" + FinalAmount;
            RateChosen = WeekInterestRate;
            InterestAmount = WeekInterestAmount;
            Days = WEEK;
        }

【问题讨论】:

    标签: c#


    【解决方案1】:
    if (radioButton2.Checked == true)
    {
        FinalAmount = (Amount + WeekInterestAmount);
        label10.Text = "Total Amount After 7 Days" + " " + "€" + FinalAmount.ToString("N2");
        RateChosen = WeekInterestRate;
        InterestAmount = WeekInterestAmount;
        Days = WEEK;
    }
    

    【讨论】:

      【解决方案2】:

      这个:

      FinalAmount.ToString("N2");
      

      实际上并没有改变 FinalAmount。

      string finald2 = FinalAmount.ToString("N2");
      label10.Text = "Total Amount After 7 Days" + " " + "€" + finald2;
      

      试试吧。

      【讨论】:

        【解决方案3】:

        你没有对FinalAmount.toString("N2") 做任何事情,因为它不会修改原始数量,只是它的表示。试试这个:

        label10.Text = "Total Amount After 7 Days" + " " + "€" + FinalAmount.ToString("N2");
        

        【讨论】:

          【解决方案4】:

          这一行什么都不做:

          FinalAmount.ToString("N2");
          

          嗯,实际上它做了一些事情。它将值格式化为字符串,但结果会被丢弃。

          在您将其放入标签的行中使用该代码:

          label10.Text = "Total Amount After 7 Days" + " " + "€" + FinalAmount.ToString("N2");
          

          【讨论】:

            【解决方案5】:
            public void RadioButtons()
            {
            
                if (radioButton2.Checked == true)
                {
                    FinalAmount = (Amount + WeekInterestAmount);
                    label10.Text = "Total Amount After 7 Days" + " " + "€" + FinalAmount.ToString("N2");
                    RateChosen = WeekInterestRate;
                    InterestAmount = WeekInterestAmount;
                    Days = WEEK;
                }
            

            【讨论】:

              【解决方案6】:
              label10.Text = string.Format("Total Amount After 7 Days {0:C}", FinalAmount);
              

              这将自动将FinalAmount 格式化为带有货币符号和2 个小数位的货币。与N2 一样,它使用CurrentCulture 来确定千位分隔符、货币符号和小数点分隔符。

              或者,您可以继续硬编码货币符号并将其更改为:

              label10.Text = string.Format("Total Amount After 7 Days €{0:N2}", FinalAmount);
              

              【讨论】:

                猜你喜欢
                • 2010-11-20
                • 1970-01-01
                • 2020-09-11
                • 2022-12-07
                • 2021-06-05
                • 2011-06-14
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多