MS Access 中的舍入 [英] Rounding in MS Access

查看:31
本文介绍了MS Access 中的舍入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VBA Access 中最好的舍入方式是什么?

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 函数使用 Banker 的舍入,将 .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 向上舍入(对称舍入)也用于单元格格式,也用于列宽舍入(当使用通用数字格式时).显示的精度"标志本身似乎没有进行任何舍入,它只是使用单元格格式的舍入结果.

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.

我尝试在 VBA 中实现 Microsoft 的 SymArith 函数进行四舍五入,但是当您尝试给它一个像 58.55 这样的数字时发现 Fix 有错误;函数给出的结果是 58.5 而不是 58.6.然后我终于发现你可以使用 Excel Worksheet 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.

这篇关于MS Access 中的舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆