数字不会到两位小数

时间:2014-02-19 23:12:49

标签: c#

我正在申请兴趣。我试图将总金额的利息金额提高到2位小数。我在代码中一直使用("N2"),除了标签中的一个区域外,它已经工作了。

我使用标签显示Radiobutton()区域中的总金额,它仍然显示完整的数字eg.14.8282423432,而不是2位小数。有办法解决这个问题吗?感谢。

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

6 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

此:

FinalAmount.ToString("N2");

根本没有改变FinalAmount。

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

尝试改为。

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

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

答案 4 :(得分:1)

此行没有任何作用:

FinalAmount.ToString("N2");

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

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

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

答案 5 :(得分:1)

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