送分啊!请问在vb中的四舍五入函数是哪一个,或者相关函数?(200分)

  • 主题发起人 主题发起人 zjok
  • 开始时间 开始时间
Z

zjok

Unregistered / Unconfirmed
GUEST, unregistred user!
不是我不看帮助,是找不到啊!请帮忙!
 
描述<br>返回按指定位数进行四舍五入的数值。<br>语法<br>Round(expression[, numdecimalplaces])<br>Round 函数的语法有以下参数:<br><br>参数 描述 <br>expression 必选项。 数值表达式 被四舍五入。 <br>numdecimalplaces 可选项。数字表明小数点右边有多少位进行四舍五入。如果省略,则 Round 函数返回整数。 &nbsp;<br><br><br>下面的示例利用 Round 函数将数值四舍五入到两位小数: <br>Dim MyVar, pi<br>pi = 3.14159<br>MyVar = Round(pi, 2) 'MyVar contains 3.14。<br><br>
 
&nbsp; &nbsp;Public Function Round45(n As Double, p As Integer) As Double <br>&nbsp; &nbsp; &nbsp;Dim s As String, a As String, b As String, x As String, v As Double <br>&nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp;If n = 0 Or p &lt; 0 Then Round45 = "": Exit Function <br>&nbsp; &nbsp; &nbsp;s = Trim(Str(n)) <br>&nbsp; &nbsp; &nbsp;If InStr(s, ".") &lt;&gt; 0 Then <br>&nbsp; &nbsp; &nbsp;a = Mid(s, 1, InStr(s, ".") - 1) <br>&nbsp; &nbsp; &nbsp;b = Mid(s, InStr(s, ".") + 1) <br>&nbsp; &nbsp; &nbsp;Else <br>&nbsp; &nbsp; &nbsp;a = s <br>&nbsp; &nbsp; &nbsp;b = "" <br>&nbsp; &nbsp; &nbsp;End If <br>&nbsp; &nbsp; &nbsp;If Len(b) &lt;= p Then <br>&nbsp; &nbsp; &nbsp;Round45 = CDbl(Val(a + "." + b)) <br>&nbsp; &nbsp; &nbsp;Exit Function <br>&nbsp; &nbsp; &nbsp;Else <br>&nbsp; &nbsp; &nbsp;If Val(Mid(b, p + 1, 1)) &gt;= 5 Then <br>&nbsp; &nbsp; &nbsp;x = a + Mid(b, 1, p) <br>&nbsp; &nbsp; &nbsp;v = CDbl(Val(x)) <br>&nbsp; &nbsp; &nbsp;v = v + 1# <br>&nbsp; &nbsp; &nbsp;v = v / (10 ^ p) <br>&nbsp; &nbsp; &nbsp;Round45 = v <br>&nbsp; &nbsp; &nbsp;Exit Function <br>&nbsp; &nbsp; &nbsp;Else <br>&nbsp; &nbsp; &nbsp;Round45 = CDbl(Val(a + "." + b)) <br>&nbsp; &nbsp; &nbsp;Exit Function <br>&nbsp; &nbsp; &nbsp;End If <br>&nbsp; &nbsp; &nbsp;End If <br>&nbsp; &nbsp; End Function <br>[red]可别用Round,至少在VB6中它不是四舍五入<br>Round函数实行Banker舍入,而不是我们习惯的算术舍入(四舍五入)。[/red]
 
Function NewInt(Mi_PRS_Num As Double) As Variant <br>'若保留两位,则: <br>NewInt=Int(Mi_PRO_Num*100+0.5)/100 &nbsp; //可以根据需要位数不同自己修改<br>End Function
 
Round(number[,numdecimalplaces])<br>说明:number是任何有效的数值运算式;<br>numdecimalplaces是四舍五入到第几位小数,若省略则舍入到整数<br><br>Round(3.635)=4<br>Round(3.635,1)=3.6
 
好像没有很直接的,就这样做吧<br>private Function FourFive(byval dblVal as Double) AS long<br>dim lngVal1 as long<br>dim lngVal2 as long <br>dim dblVal2 as double<br>&nbsp; &nbsp; lngVal1= FIX(dblVal) '`不大于自己的整数<br>&nbsp; &nbsp; dblVal2=dblVal-lngVal1 &nbsp; &nbsp; ' 取小数部分<br>&nbsp; &nbsp; if dblVal2=0 then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FourFive=lngVal1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit function<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dblVal2=dblVal2*100<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if dblVal2&gt;=50 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lngVal2=1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lngVal2=0<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end if<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if dblVal&lt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FourFive=lngVal1+lngVal2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FourFive=lngVal1-lngVal2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end if<br>&nbsp; &nbsp; end if<br>end function &nbsp;<br>&nbsp; &nbsp;
 
实在找不到自己写一个也很容易的!<br>function n2int(number:float):integer;<br>begin<br>&nbsp;result:=int(number+0.5)<br>end
 
原来vb真的没有四舍五入函数啊!<br>还有没有其它方法,收集一下吧!<br>对一些好的方法,我可另外加分啊!<br>分数不成问题!
 
有直接的函数啊,Round() 函数啊<br>ysai说的没错
 
x=3.1415926<br>Round(x,2)
 
Round() 函数在VB中只是舍没有入,即Round(3.1415,2)=3.14,Round(3.146,2)=3.14。<br>用在其他则可以,如SQL,DELPHI,我实在找不到直接的函数。
 
可以用楼上几为仁兄提供的自己写的函数,<br>自己写也很简单的。
 
后退
顶部