首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在MS Access中安装MROUND函数?

如何在MS Access中安装MROUND函数?
EN

Stack Overflow用户
提问于 2019-11-04 05:30:28
回答 2查看 436关注 0票数 2

此函数MRound在MS Access中不存在,您不能只安装它。

我正尝试在Access报告中使用mround函数。该函数未在内置函数的表达式生成器框中列出。当我使用计算,例如,=mround([AmountDue],0.05),并运行报告时,它要求提供参数mround

EN

回答 2

Stack Overflow用户

发布于 2019-11-04 06:25:12

您可以在公共VBA模块中定义自己的模块,例如:

代码语言:javascript
复制
Function MRound(dblNum As Double, dblMtp As Double) As Double
    MRound = dblMtp * Round(dblNum / dblMtp, 0)
End Function

或者,将对Microsoft Excel对象库的引用添加到VBA工程中(工具>引用),并将公共VBA模块中的MRound函数定义为:

代码语言:javascript
复制
Function MRound(dblNum As Double, dblMtp As Double) As Double
    MRound = Excel.WorksheetFunction.MRound(dblNum, dblMtp)
End Function
票数 4
EN

Stack Overflow用户

发布于 2019-11-04 18:58:49

Access中没有MRound,因此请创建您自己的舍入函数。

但是,千万不要使用Round,因为它有臭名昭著的buggy。因此,请使用普通数学和数据类型Decimal来避免错误:

代码语言:javascript
复制
' Rounds a value by 4/5 to the nearest multiplum of a rounding value.
' Accepts any value within the range of data type Currency.
' Mimics Excel function MRound without the limitations of this.
'
' Examples:
'
'   RoundAmount(-922337203685477.5808, 0.05)    -> -922337203685477.6
'   RoundAmount( 922337203685477.5807, 0.05)    ->  922337203685477.6
'   RoundAmount( 10, 3)                         ->                9
'   RoundAmount(-10,-3)                         ->               -9
'   RoundAmount( 1.3, 0.2)                      ->                1.4
'   RoundAmount( 122.25, 0.5)                   ->              122.5
'   RoundAmount( 6.05, 0.1)                     ->                6.1
'   RoundAmount( 7.05, 0.1)                     ->                7.1

' 2009-05-17. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function RoundAmount( _
    ByVal Value As Currency, _
    ByVal RoundValue As Currency) _
    As Variant

    Dim BaseValue   As Variant
    Dim Result      As Variant

    BaseValue = Int(Value / CDec(RoundValue) + CDec(0.5))
    Result = BaseValue * RoundValue

    RoundAmount = Result

End Function

例如,此函数将正确地舍入以下内容:

代码语言:javascript
复制
Amount = RoundAmount( 122.25, 0.5)
Amount -> 122.50

有关精确舍入的详细信息,请参阅我在GitHub上的项目

VBA.Round

以及本文所指的文章。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58684800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档