我想创建一个简单的应用程序,它可以让你输入你的薪水,然后当按钮被按下时,薪水税将被计算和显示出来。该应用程序的作品,但我希望结果被迫显示两位小数。我尝试了不同的代码,但它不会这样做。 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