-1

I'm coding a simple calculator, and it needs to be displaying 2 digits after the decimal, but I can't figure it out. The rest of the program works perfectly, and so does the quotient button, but it's just displaying too many digits after the decimal. Here's what I have:

private double _leftop = 0;
private double _rightop = 0;
private double _quotient = 0;

    private void BtnQuotient_Click(object sender, EventArgs e)
    {
        if (Double.TryParse(TxtLeftOperand.Text, out _leftop) == true &&     Double.TryParse(TxtRightOperand.Text, out _rightop) == true)
        {
            _quotient = _leftop / _rightop;
            TxtResult.Text = Convert.ToString(_quotient);
        }
        else
        {
            TxtResult.Text = "Error";
        }
    }
  • 1
    Do you want to _display_ with two digits, or convert to two digits? The former you can use a `Format` on the output to a string, the latter as was posted as answer below, you can use `Math.Round()`. – Jane S Jan 13 '15 at 02:47
  • `TextResult.Text = _quotient.ToString("f2");` – DhavalR Jan 13 '15 at 05:31

2 Answers2

0
TxtResult.Text = (Math.Round(_quotient, 2)).ToString();
Abhishek
  • 2,925
  • 4
  • 34
  • 59
Shahar
  • 1,687
  • 2
  • 12
  • 18
0

Use Math.Round() method.

Math.Round(value, numOfDigits);
Abhishek
  • 2,925
  • 4
  • 34
  • 59