How to round a number in Visual Basic to n decimal places
In this example I will show you how to take a decimal number in Visual Basic and round it to a specified number of decimal places. Follow the example below.
Start Visual Basic, by selecting it from the start menu.
When VB has loaded, a window appears. Click on Standard-EXE which will create form1 by default.
On the toolbox to the right of form1 select the command button and drag it to a resonable size on the form.
Open up form1注释:s code window by double clicking on the command button.
A new window will appear. Delete everything in that window and type the following:
Private Sub Command1_Click()
MsgBox Round(12.3265, 2)
End Sub
Function Round(nValue As Double, nDigits As _
Integer) As Double
Round = Int(nValue * (10 ^ nDigits) + _0.5) / (10 ^ nDigits)
End Function
|