我想創建一個簡單的應用程序,它可以讓你輸入你的薪水,然後當按鈕被按下時,薪水稅將被計算和顯示出來。該應用程序的作品,但我希望結果被迫顯示兩位小數。我嘗試了不同的代碼,但它不會這樣做。 Math.Round(x,2);是最明顯的,但不起作用。有人能告訴我帽子我做錯了嗎?C#強制結果到兩位小數不起作用
我的代碼:
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)
{
}
}
}
解釋'不起作用'。最有可能的是,你正在談論四捨五入的方向。還有第三個參數可以傳入,這是一個枚舉。嘗試'AwayFromZero'進行正常的四捨五入。 – ps2goat 2015-04-03 13:21:00
工資不應該是雙倍的。無處不在用於財務計算。要格式化輸出,請使用[String.Format](https://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx)。 – LarsTech 2015-04-03 13:22:33
Duplicate:http://stackoverflow.com/questions/3602392/round-double-to-two-decimal-places – 2015-04-03 13:25:44