C# 将结果强制为两位小数不起作用

C# Forcing result to two decimals doesn't work

我正在尝试创建一个简单的应用程序,让您输入薪水,然后在按下按钮时计算并显示薪水税。该应用程序有效,但我希望结果强制显示两位小数。我尝试了不同的代码,但它不会这样做。 Math.Round (x, 2);最明显,但不起作用。谁能告诉我我做错了什么?

我的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }


    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        String Text = Textbox.Text;
        Double Salaris = Convert.ToDouble(Text);
        Double Belasting;
        Double Schijf1;
        Double Schijf2;
        Double Schijf3;
        Double Schijf4;

        if (Salaris <= 6800)
        {
          Belasting = Math.Round (((Salaris / 100) * 35.70) ,2);
          Schijf1 = Math.Round (((Salaris / 100) * 35.70) ,2);
            S1uitkomst.Content = Schijf1;
            S2uitkomst.Content = 0000.00;
            S3uitkomst.Content = 0000.00;
            S4uitkomst.Content = 0000.00;
        }

        else if (Salaris > 6800 && Salaris < 21800)
        {
          Belasting = Math.Round (((6800 / 100) * 35.70) + (((Salaris - 6800) / 100) * 37.05) ,2);
          Schijf1 = Math.Round (((6800 / 100) * 35.70) ,2);
          Schijf2 = Math.Round ((((Salaris - 6800) / 100) * 37.05) ,2);
            S1uitkomst.Content = Schijf1;
            S2uitkomst.Content = Schijf2;
            S3uitkomst.Content = 0000.00;
            S4uitkomst.Content = 0000.00;
        }

        else if (Salaris > 21800 && Salaris < 48100)
        {
          Belasting = Math.Round (((6800 / 100) * 35.70) + ((15000 / 100) * 37.05) + (((Salaris - 21800) / 100) * 50.00) ,2);
          Schijf1 = Math.Round (((6800 / 100) * 35.70), 3);
          Schijf2 = Math.Round (((15000 / 100) * 37.05) ,3);
          Schijf3 = Math.Round ((((Salaris - 21800) / 100) * 50.00) ,3);
            S1uitkomst.Content = Schijf1;
            S2uitkomst.Content = Schijf2;
            S3uitkomst.Content = Schijf3;
            S4uitkomst.Content = 0000.00;

        }

        else
        {
          Belasting = Math.Round (((6800 / 100) * 35.70) + ((15000 / 100) * 37.05) + ((26300 / 100) * 50.00) + (((Salaris - 48100) / 100) * 60.00) ,2);
          Schijf1 = Math.Round (((6800 / 100) * 35.70), 2);
          Schijf2 = Math.Round (((15000 / 100) * 37.05) ,2);
          Schijf3 = Math.Round (((26300 / 100) * 50.00), 2);
          Schijf4 = Math.Round ((((Salaris - 48100) / 100) * 60.00) ,2);
            S1uitkomst.Content = Schijf1;
            S2uitkomst.Content = Schijf2;
            S3uitkomst.Content = Schijf3;
            S4uitkomst.Content = Schijf4;
        }

        Totaal.Content = Belasting;



    }

    private void Textbox_TextChanged(object sender, TextChangedEventArgs e)
    {

    }
}

}

您需要将结果转换成字符串:

S2uitkomst.Content = String.Format("{0:0.00}", Schijf2 );

如果那里没有零,你将永远不会看到显示 2 个或更多零的 double。这没有意义,因为 double 不存储没有价值的数字。

但是,如果您的目标是显示带两位小数的用户实际数字,只需将其转换为字符串即可:

var result = string.Format("{0:0.00}", doubleVariable);

我的猜测(因为你的问题并不清楚确切的问题是什么)是你没有在输出的两位数字后截断小数点。您需要格式化输出而不是舍入基础值。 Round 函数只是告诉你的十进制值精度较低,而不是显示不同。你所说的你想要的是你希望你的价值以一定的精度显示出来。 ToString method of Double 有你需要的

您可以删除 Round 调用并执行类似

的操作
S1uitkomst.Content = Schijf1.toString("C");

这会将您的输出格式化为货币。如果您想以不同的方式控制格式,请查看 ToString 方法的文档。

由于您处理的是货币,因此可以采用包含美元符号的格式。

以 SO 答案为例。 Formatting Output to Currency