2015-01-13 48 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); 
相關問題