0

I'm trying to create a simple application which lets you enter your salary and then when the button is pressed the taxes on the salary will ben calculated and shown. The app works, but I want the results to be forced showing two decimals. I've tried different codes, but it won't do it. Math.Round (x , 2); is most obvious, but doesn't work. Can someone tell me hat I'm doing wrong?

My code:

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

    }
}

}

Sjoerd1234
  • 148
  • 1
  • 2
  • 11
  • 1
    explain `doesn't work`. Most likely, you are talking about the direction of the rounding. There's a third parameter you can pass in, which is an enum. Try `AwayFromZero` for normal rounding. – ps2goat Apr 03 '15 at 13:21
  • 4
    Salaries shouldn't be Double. Use Decimal everywhere for financial calculations. For formatting the output, use [String.Format](https://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx). – LarsTech Apr 03 '15 at 13:22
  • Duplicate: http://stackoverflow.com/questions/3602392/round-double-to-two-decimal-places – Tim Rutter Apr 03 '15 at 13:25

4 Answers4

0

You need to convert the result into a string:

S2uitkomst.Content = String.Format("{0:0.00}", Schijf2 );
Tim Rutter
  • 4,549
  • 3
  • 23
  • 47
0

You will never see double showing 2 or more zeros, if there is no zeros there. And that doesn't make sense, since double doesn't store numbers, that have no value.

But, if you goal is to show user actual number with two decimal points, just convert it to string:

var result = string.Format("{0:0.00}", doubleVariable);
Yura
  • 2,013
  • 19
  • 25
0

My guess (since what the exact problem is not clear from your question) is that you are not getting the decimal cut off after two digits on the output. You need to format your output not round the underlying value. The Round function is only telling your decimal value to have less precision, not display differently. What you are saying you want is you want your value to display with a certain percision. The ToString method of Double has what you need

You can remove your Round calls and do something like

S1uitkomst.Content = Schijf1.toString("C");

This will format your output as currency. Look at the documentation of the ToString method if you want to control the formatting in different ways.

Craig Suchanec
  • 10,474
  • 3
  • 31
  • 39
0

Since you're dealing with currency, you can format it that way which would include the dollar sign.

See SO answer as an example. Formatting Output to Currency

Community
  • 1
  • 1
Shar1er80
  • 9,001
  • 2
  • 20
  • 29