2013-03-21 135 views
1

我有一個計算器的錐體積的Visual Basic計算器。如何顯示答案一旦答案超過2位小數

我有兩個答案,一個是毫米立方體,另一個是米立方體(四捨五入到最接近的百分之一(小數點後兩位))。但我只想在米數達到0.01或更高時顯示米。

這是我的運算用代碼

Private Sub txtSidea_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtHeight.Leave, txtRadius.Leave 
    'calculate volume cubic mm using V= 1/3 pi R*2*H 
    Const PI As Double = System.Math.PI 
    lbAnswerlVolumeMM.Text = (1/3 * PI * ((Val(txtRadius.Text)^2) * Val(txtHeight.Text))).ToString("#") + " :Cuibic mm" 
    'calculate volume cubic meter using V= 1/3 pi R*2*H/10^9 
    lblAnswerVolumeMetres.Text = ((1/3 * PI * ((Val(txtRadius.Text)^2) * Val(txtHeight.Text)))/10^9).ToString("#.##") + " :Cubic Metre" 
End Sub 

一個例子是;高度50毫米,半徑30毫米。這將輸出47124毫米立方體,這是很好的。但它不顯示任何米的立方體,所以如果答案在0.01立方米以下,我希望它隱藏標籤,直到超過0.01,然後顯示結果。

+0

請共享樣本輸入和輸出 – 2013-03-21 05:13:30

+0

也許檢查是否該值高於0.01或低於0.01,並使用基於該不同的計算? – Patashu 2013-03-21 05:14:52

+0

我被沿着線作爲 – user1427806 2013-03-21 05:42:07

回答

0

嘗試:

Private Sub txtSidea_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtHeight.Leave, txtRadius.Leave 
    'calculate volume cubic mm using V= 1/3 pi R*2*H 
    Const PI As Double = System.Math.PI 
    lbAnswerlVolumeMM.Text = (1/3 * PI * ((Val(txtRadius.Text)^2) * Val(txtHeight.Text))).ToString("#") 
    'calculate volume cubic meter using V= 1/3 pi R*2*H/10^9 
    lblAnswerVolumeMetres.Text = ((1/3 * PI * ((Val(txtRadius.Text)^2) * Val(txtHeight.Text)))/10^9).ToString("#.##") 

    '--Decide which to hide 
    If lblAnswerVolumeMetres.Text >= 0.01 then 
     lblAnswerVolumeMetres.Visible = True 
     lbAnswerlVolumeMM.Visible = False 
    Else 
     lblAnswerVolumeMetres.Visible = False 
     lbAnswerlVolumeMM.Visible = True 
    End If 
    '--Shift the description to after the decision is made based on the number. 
    lblAnswerVolumeMetres.Text &= " :Cubic mm" 
    lbAnswerlVolumeMM &= " :Cubic Metre" 
End Sub 
+0

它的工作原理,但是當我輸入我的號碼到txtSidea我得到這個錯誤。從字符串「」轉換爲「Double」類型無效。 – user1427806 2013-03-21 06:09:02

+0

此代碼沒有工作 – user1427806 2013-03-21 06:37:40

+0

讓它工作。 – user1427806 2013-03-21 07:06:05