2015-01-13 43 views
-1

我正在编码一个简单的计算器,它需要显示小数点后两位数字,但我无法弄清楚。该程序的其余部分完美工作,商标按钮也是如此,但它只是在小数点后显示太多数字。下面是我有:如何在C#中使用双精度型数字获得小数点后两位数的数字?

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

你想用两位数显示_或者转换成两位数吗?前者你可以在输出上使用'Format'作为字符串,后者可以用下面的答案发表,你可以使用'Math.Round()'。 –

+0

'TextResult.Text = _quotient.ToString(“f2”);' – DhavalR

回答

0
TxtResult.Text = (Math.Round(_quotient, 2)).ToString(); 
0

使用Math.Round()方法。

Math.Round(value, numOfDigits); 
相关问题