回 帖 发 新 帖 刷新版面

主题:!!!!!很很很很困扰我,关于round函数

查了很多资料,说round函数是四舍六入的,
也就是碰到5的情况下是看5前面的那个数字的,偶数就不进,奇进的。
我试了下,是这样的,可是为什么我再算平均数的时候,出来的结果就不是这样了??????
比如,输入1.24和1.21 ,应该出来的是1.22,可是他算的结果是1.23,输入1.24和1.27,正确的是1.26,可是他出来的结果是1.25!!!!!!!!!好困扰~!!!!!!!
我写的程序如下,用两个text输入,第三个text放结果。一个计算按钮。

Private Sub Command1_Click()
Text3.Text = Round((Val(Text1.Text) + Val(Text2.Text)) / 2, 2)
End Sub
------------------------
好困惑阿~~~~~~急急急!!!!

回复列表 (共6个回复)

沙发

round 确实是有这个问题 所以偶一直用 fromat
msgbox format(1.273,"0.00")
msgbox format(1.275,"0.00")

板凳

更建议自己写一个通用函数来做这件事。

对Round函数而言,反复测试,明显有规律,但却不是我们需要的规律。

3 楼


MSDN上有篇文章有关于这个问题的说明:

PRB: Round Function different in VBA 6 and Excel Spreadsheet
ID: Q194983 

 

--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 6.0 
Microsoft Excel 97 for Windows

--------------------------------------------------------------------------------


SYMPTOMS
When using the Round() function in Visual Basic 6.0, a different result may be returned than when using it in a cell formula of an Excel spreadsheet. 



CAUSE
The VBA Round() function uses Banker's rounding while the spreadsheet cell function uses arithmetic rounding. 



RESOLUTION
Write a custom function to get the desired results. 



STATUS
This behavior is by design. 



MORE INFORMATION
The Round() function in an Excel spreadsheet uses Arithmetic rounding, which always rounds .5 up (away from 0). The Round() function in Visual Basic for Applications 6, uses Banker's rounding, which rounds .5 either up or down, whichever will result in an even number. 

Steps to Reproduce Behavior
In Excel, open a new spreadsheet and type the following formula into one of the cells: 

       =Round(2.5, 0) 



The result is 3.


In Visual Basic 6.0 or other applications using VBA 6, open a new project and type the following expression into the Debug or Immediate window: 

      ? Round(2.5, 0) 



The result is 2.





REFERENCES
For additional information on rounding, click the article number below to view the article in the Microsoft Knowledge Base: 

Q196652 HOWTO: Implement Custom Rounding Procedures 



© Microsoft Corporation 1998, All Rights Reserved.
Contributions by Malcolm Stewart, Microsoft Corporation


Additional query words: 

Keywords : kbVBp600 kbGrpVBDB kbExcel97 kbVBA600 
Version : WINDOWS:6.0,97 
Platform : WINDOWS 
Issue type : kbprb 

4 楼

关于"banker's rounding",其规律是:

"四舍六入五成双",这里"四"是小于五的意思,"六"是大于五的意思,"五"是舍入位之后的尾数"恰等于5"。

5 楼

还有个奇进偶不进的性质,
不如加个简单语句
  if getvalue>=setvalue(1.225) then

    getvalue=1.23
  else
    getvalue=format(#########)
  end if

6 楼

谢谢大家!~~~
自己在vb上面需要学习的东西还和多阿~~向大家学习~~
快过年啦~~祝大家开心~~顺利~~!!!

我来回复

您尚未登录,请登录后再回复。点此登录或注册