打印本文 打印本文  关闭窗口 关闭窗口  
一个取小数位数的函数(能四舍五入)
作者:佚名  文章来源:不详  点击数  更新时间:2008/4/18 14:44:24  文章录入:杜斌  责任编辑:杜斌

  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

打印本文 打印本文  关闭窗口 关闭窗口