如何回合的MS Access,VBAMS、Access、VBA

2023-09-08 10:54:44 作者:不归路

请告诉我舍在VBA访问的最佳方法是什么?

Whats the best way to round in VBA Access?

我目前的方法是利用Excel的方法

My current method utilizes the Excel method

Excel.WorksheetFunction.Round(...

不过,我找的,不依赖于Excel中的一个手段。

But I am looking for a means that does not rely on Excel.

推荐答案

注意,在VBA Round函数使用银行家的舍入,它舍入0.5至偶数,像这样:

Be careful, the VBA Round function uses Banker's rounding, where it rounds .5 to an even number, like so:

Round (12.55, 1) would return 12.6 (rounds up) 
Round (12.65, 1) would return 12.6 (rounds down) 
Round (12.75, 1) would return 12.8 (rounds up)   

而在Excel工作表函数的轮次,总是舍0.5了。

Whereas the Excel Worksheet Function Round, always rounds .5 up.

我已经做了一些测试,它看起来像0.5了倒圆(对称舍入)也由单元格格,也为列宽四舍五入(使用常规数字格式时)。标志似乎并没有做任何四舍五入自己的所显示precision',它只是使用单元格的格式的舍入的结果。

I've done some tests and it looks like .5 up rounding (symmetric rounding) is also used by cell formatting, and also for Column Width rounding (when using the General Number format). The 'Precision as displayed' flag doesn't appear to do any rounding itself, it just uses the rounded result of the cell format.

我试图实现从微软SymArith功能VBA我倒圆,却发现当你试图给它一些像58.55的修正有一个错误;该功能提供了58.5,而不是58.6的结果。然后,我终于发现,您可以使用Excel工作表Round函数,就像这样:

I tried to implement the SymArith function from Microsoft in VBA for my rounding, but found that Fix has an error when you try to give it a number like 58.55; the function giving a result of 58.5 instead of 58.6. I then finally discovered that you can use the Excel Worksheet Round function, like so:

Application.Round(58.55,1)

这将让你做正常的四舍五入在VBA中,虽然它可能不是那么快,一些自定义的功能。我认识到,这都来自这个问题了一圈,但希望包括它的完整性。

This will allow you to do normal rounding in VBA, though it may not be as quick as some custom function. I realize that this has come full circle from the question, but wanted to include it for completeness.