一旦答案超过2位小数,如何显示答案

时间:2013-03-21 05:07:08

标签: .net vb.net number-formatting

我有一个Visual Basic计算器,用于计算圆锥的体积。

我有两个答案,一个是mm立方体,另一个是米立方体(四舍五入到最接近百分位(小数点后两位))。但是我只想在达到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

一个例子是;高度50mm,半径30mm。这将输出47124毫米立方体,这是好的。但它没有显示米立方体的任何内容,所以如果米的立方体的答案小于0.01,我希望它隐藏标签,直到它超过0.01,然后显示结果。

1 个答案:

答案 0 :(得分: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