2007-2010 年访问综述
Posted
技术标签:
【中文标题】2007-2010 年访问综述【英文标题】:Roundup in access 2007-2010 【发布时间】:2019-11-15 09:45:15 【问题描述】:我需要在 Access 2007 中汇总到 2dp,并尝试使用此论坛上类似查询的答案,但发现除了一个之外的所有错误。有没有人有一个经过测试的 VB 脚本/函数,它可以接受一个小数点后 14 位长并输出到 2 dp 的值?
【问题讨论】:
【参考方案1】:确实:
' Common constants.
'
' Base values.
Public Const Base2 As Double = 2
Public Const Base10 As Double = 10
' Enums.
'
Public Enum rmRoundingMethod
Down = -1
Midpoint = 0
Up = 1
End Enum
' Rounds Value up with count of decimals as specified with parameter NumDigitsAfterDecimal.
'
' Rounds to integer if NumDigitsAfterDecimal is zero.
'
' Optionally, rounds negative values away from zero.
'
' Uses CDec() to prevent bit errors of reals.
'
' Execution time is about 0.5µs for rounding to integer,
' else about 1µs.
'
' 2018-02-09. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function RoundUp( _
ByVal Value As Variant, _
Optional ByVal NumDigitsAfterDecimal As Long, _
Optional ByVal RoundingAwayFromZero As Boolean) _
As Variant
Dim Scaling As Variant
Dim ScaledValue As Variant
Dim ReturnValue As Variant
' Only round if Value is numeric and ReturnValue can be different from zero.
If Not IsNumeric(Value) Then
' Nothing to do.
ReturnValue = Null
ElseIf Value = 0 Then
' Nothing to round.
' Return Value as is.
ReturnValue = Value
Else
If NumDigitsAfterDecimal <> 0 Then
Scaling = CDec(Base10 ^ NumDigitsAfterDecimal)
Else
Scaling = 1
End If
If Scaling = 0 Then
' A very large value for NumDigitsAfterDecimal has minimized scaling.
' Return Value as is.
ReturnValue = Value
ElseIf RoundingAwayFromZero = False Or Value > 0 Then
' Round numeric value up.
If Scaling = 1 Then
' Integer rounding.
ReturnValue = -Int(-Value)
Else
' First try with conversion to Decimal to avoid bit errors for some reals like 32.675.
On Error Resume Next
ScaledValue = -Int(CDec(-Value) * Scaling)
ReturnValue = ScaledValue / Scaling
If Err.Number <> 0 Then
' Decimal overflow.
' Round Value without conversion to Decimal.
ScaledValue = -Int(-Value * Scaling)
ReturnValue = ScaledValue / Scaling
End If
End If
Else
' Round absolute value up.
If Scaling = 1 Then
' Integer rounding.
ReturnValue = Int(Value)
Else
' First try with conversion to Decimal to avoid bit errors for some reals like 32.675.
On Error Resume Next
ScaledValue = Int(CDec(Value) * Scaling)
ReturnValue = ScaledValue / Scaling
If Err.Number <> 0 Then
' Decimal overflow.
' Round Value without conversion to Decimal.
ScaledValue = Int(Value * Scaling)
ReturnValue = ScaledValue / Scaling
End If
End If
End If
If Err.Number <> 0 Then
' Rounding failed because values are near one of the boundaries of type Double.
' Return value as is.
ReturnValue = Value
End If
End If
RoundUp = ReturnValue
End Function
示例:
Value14 = CDec("12345.00000000000001")
Value02 = RoundUp(Value14, 2)
Value02 -> 12345.01
您也可以研究我关于以极高的精度进行舍入的文章:
Rounding values up, down, by 4/5, or to significant figures
(如果您没有帐户,请浏览链接:阅读全文)
完整代码也在 GitHub 上:VBA.Round
【讨论】:
嗨,我以 Roundup(0.1234,2) 格式传递此函数值,并将其设置缩放为零,因此返回值而不进行舍入。我不知道为什么,我需要将值作为文本传递吗? 缩放?请发布您的完整代码(附加到您的问题中)。 我将您的代码与 Roundup(0.1234,2) 一起使用 如果每次都将缩放设置为 0: If NumDigitsAfterDecimal 0 Then Scaling = CDec(Base10 ^ NumDigitsAfterDecimal) 返回0.13
。
- 假设点是小数点分隔符。听起来您使用逗号作为小数点分隔符。以上是关于2007-2010 年访问综述的主要内容,如果未能解决你的问题,请参考以下文章