Husna5207 Husna5207 - 16 days ago 5
Vb.net Question

How to convert answer into two decimal point

This is my code and I want the output which is

txtA.Text
and
txtB.Text

to be in two decimal places.

Public Class Form1
Private Sub btncalc_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btncalc.Click
txtA.Text = Val(txtD.Text) / Val(txtC.Text) * Val(txtF.Text) / Val(txtE.Text)
txtB.Text = Val(txtA.Text) * 1000 / Val(txtG.Text)
End Sub
End Class

Answer

Try using the Format function:

Private Sub btncalc_Click(ByVal sender As System.Object,
                          ByVal e As System.EventArgs) Handles btncalc.Click
  txtA.Text = Format(Val(txtD.Text) / Val(txtC.Text) * 
                     Val(txtF.Text) / Val(txtE.Text), "0.00")
  txtB.Text = Format(Val(txtA.Text) * 1000 / Val(txtG.Text), "0.00")
End Sub