在访问中查询重要数字

问题描述 投票:0回答:1

我正在尝试在 Access 中进行查询,返回重要的数字 例如:

  • 输入23.89 输出23.9
  • 输入3.567 输出3.57
  • 输入 6.49877897 输出 6.50
  • 输入 0.0445 输出 0.045

我希望输出给出 3 位数字的输出。 或者如果没有号码可以用“ND”通知我

我的知识非常有限。 到目前为止,我最近使用的是:

roundnum: 轮([数字],4)

number          roundnum
23.89           23.89
3.567           3.567
6.49877897      6.4988
8.4532          8.4532
0.445           0.445
120             120
99.998          99.998
0.03452         0.0345
0.03568         0.0357
214.567         214.567
ms-access-2010
1个回答
0
投票

使用我在

GitHub
的存储库中找到的 RoundSignificant 函数之一:

VBA.圆形

例如:

' Rounds Value to have significant figures as specified with parameter Digits.
'
' Performs no rounding if Digits is zero.
' Rounds to integer if NoDecimals is True.
'
' Rounds correctly Value until max/min value of currency type multiplied with 10
' raised to the power of (the number of digits of the index of Value) minus Digits.
' This equals roughly +/-922 * 10 ^ 12 for any Value of Digits.
'
' Uses CDec() to prevent bit errors of reals.
'
' For rounding of values reaching the boundaries of type Currency, use the
' function RoundSignificantDec.
'
' Requires:
'   Function Log10.
'
' 2018-02-09. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function RoundSignificantCur( _
    ByVal Value As Currency, _
    ByVal Digits As Integer, _
    Optional ByVal NoDecimals As Boolean, _
    Optional ByVal MidwayRoundingToEven As Boolean) _
    As Variant
    
    Dim Exponent    As Double
    Dim Scaling     As Double
    Dim Half        As Variant
    Dim ScaledValue As Variant
    Dim ReturnValue As Currency
    
    ' Only round if Value is numeric and result can be different from zero.
    If Not IsNumeric(Value) Then
        ' Nothing to do.
        ReturnValue = Null
    ElseIf (Value = 0 Or Digits <= 0) Then
        ' Nothing to round.
        ' Return Value as is.
        ReturnValue = Value
    Else
        ' Calculate scaling factor.
        Exponent = Int(Log10(Abs(Value))) + 1 - Digits
        If NoDecimals = True Then
            ' No decimals.
            If Exponent < 0 Then
                Exponent = 0
            End If
        End If
        Scaling = Base10 ^ Exponent
        
        If Scaling = 0 Then
            ' A very large value for Digits has minimized scaling.
            ' Return Value as is.
            ReturnValue = Value
        Else
            ' Very large values for Digits can cause an out-of-range error when dividing.
            On Error Resume Next
            ScaledValue = CDec(Value) / Scaling
            If Err.Number <> 0 Then
                ' Return value as is.
                ReturnValue = Value
            Else
                ' Perform rounding.
                If MidwayRoundingToEven = False Then
                    ' Round away from zero.
                    Half = CDec(Sgn(Value) / 2)
                    ReturnValue = CCur(Fix(ScaledValue + Half) * Scaling)
                Else
                    ' Round to even.
                    ReturnValue = CCur(Round(ScaledValue) * Scaling)
                End If
                If Err.Number <> 0 Then
                    ' Rounding failed because values are near one of the boundaries of type Currency.
                    ' Return value as is.
                    ReturnValue = Value
                End If
            End If
        End If
    End If
  
    RoundSignificantCur = ReturnValue

End Function

辅助功能:

Option Explicit

' Common constants.
'
' Base values.
Public Const Base2      As Double = 2
Public Const Base10     As Double = 10

' Returns Log 10 of Value.
'
' 2018-02-09. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Log10( _
    ByVal Value As Double) _
    As Double

    ' No error handling as this should be handled
    ' outside this function.
    '
    ' Example:
    '
    '     If MyValue > 0 then
    '         LogMyValue = Log10(MyValue)
    '     Else
    '         ' Do something else ...
    '     End If
    
    Log10 = Log(Value) / Log(Base10)

End Function
© www.soinside.com 2019 - 2024. All rights reserved.