如何在 Access 2013 中对数字进行 ROUNDUP?

How to ROUNDUP a number in Access 2013?

对于 Access 2013,我需要一种方法来在 SQL 查询中将任何小数舍入到下一个整数。

示例:

SELECT ROUNDUP(NumberValues) FROM Table1

上述查询中,1.25的行查询后应该return为2

据我所知,Access 2013 中没有用于 SQL 查询语句的 ROUNDUP 函数。

我从这个 link 中找到了一个等价的 ROUNDUP: http://allenbrowne.com/round.html#RoundUp

To round upwards towards the next highest number, take advantage of the way Int() rounds negative numbers downwards, like this: - Int( - [MyField])

As shown above, Int(-2.1) rounds down to -3. Therefore this expression rounds 2.1 up to 3.

To round up to the higher cent, multiply by -100, round, and divide by -100: Int(-100 * [MyField]) / -100

语法违反直觉,但它完全符合我的预期。

我发现在访问中对数字进行四舍五入的最简单方法是使用这样的四舍五入函数:

轮([我的字段]+0.4,0)

例如,数字 10.1 将变为 10.5。应用round函数时,四舍五入到11。如果数字是10.9,加上0.4变成11.3,四舍五入到11。

出色的回答'alextansc'。这个 public 小功能很有用:

Public Function GlblRoundup(wNumber As Currency, wDecPlaces As Integer)  As Currency

Dim wResult As Currency
Dim wFactor As Currency

    Select Case wDecPlaces
        Case 0
            wFactor = -1
        Case 1
            wFactor = -10
        Case 2
            wFactor = -100
        Case 3
            wFactor = -1000
        Case 4
            wFactor = -10000
        Case Else
            wFactor = -10000
    End Select

    wResult = Int(wFactor * wNumber) / wFactor

    GlblRoundup = Round(wResult, wDecPlaces)

End Function

这也很好用

Public Function roundUp(dValue As Double, idecimal As Integer) As Double
    Dim iSign As Integer
    If dValue < 0 Then
       iSign = -1
    Else
       iSign = 1
    End If
    dValue = Abs(dValue)

    If Round(dValue, 0) = 0 Then
        roundUp = 1 / 10 ^ idecimal * iSign
    Else
      roundUp = Round(dValue + 4 / 10 ^ (idecimal + 1), idecimal) * iSign

    End If

结束函数

汇总示例 (10.333,2)=10.34

这里简单易懂:

Public Function roundUp(ByVal theValue As Long) As Integer
    Dim tempInt As Integer
    tempInt = theValue 'cast value to whole integer
    If (tempInt = theValue) Then 'check if original value was already whole
        'do nothing
    Else
        tempInt = tempInt + 1 'value was not whole integer, add one to round up
    End If
    roundUp = tempInt 'return rounded value
End Function

注意: 在调用该函数之前,不要忘记检查您的值是否为空! 此函数会将最小到最大的小数点四舍五入到下一个整数!

整数向上取整示例:

If n > Round ( n, 0 ) then
    n = Round ( n, 0 ) + 1    
Else    
    n = Round ( n, 0 )    
End If

您只需使用 圆(字段+0.004999,2) 要么 圆(字段+0.000499,3) ...

Public Function GblRoundUp(wNumber As Double, wDecPlaces As Integer) As Double
    Dim wResult As Double
    Dim wFactor As Double
    wFactor = -10 ^ wDecPlaces
    wResult = Int(wFactor * wNumber) / wFactor
    GblRoundUp = Round(wResult, wDecPlaces)
End Function