VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 一个取小数位数的函数(能四舍五入)
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:15613, 日期:2000-11-10
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