SQL 问:如何在 Access 2013 中四舍五入一个数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31980106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:56:35  来源:igfitidea点击:

Q: How to ROUNDUP a number in Access 2013?

sqlroundingms-access-2013

提问by alextansc

For Access 2013, I need a way to round up any fractional numbers to the next whole number in an SQL query.

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

Example:

例子:

SELECT ROUNDUP(NumberValues) FROM Table1

In the above query, a row with 1.25 should return as 2 after querying.

在上面的查询中,1.25 的行在查询后应该返回 2。

As far as I know, there's no ROUNDUP function in Access 2013 for use in a SQL query statement.

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

回答by alextansc

I found a ROUNDUP equivalent from this link: http://allenbrowne.com/round.html#RoundUp

我从这个链接找到了一个 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

要向上舍入到下一个最高数字,请利用 Int() 向下舍入负数的方式,如下所示: - Int( - [MyField])

如上所示,Int(-2.1) 向下舍入为 -3。因此,该表达式将 2.1 舍入为 3。

要四舍五入到更高的分,乘以 -100,四舍五入,然后除以 -100: Int(-100 * [MyField]) / -100

The syntax is counter-intuitive, but it works exactly as I intended.

语法是违反直觉的,但它完全按照我的意图工作。

回答by Felix

I have found the easiest way to round up a number in access is to use the round function like this:

我发现在 access 中对数字进行四舍五入的最简单方法是使用 round 函数,如下所示:

Round([MyField]+0.4,0)

回合([MyField]+0.4,0)

The number 10.1, for example then becomes 10.5. When the round function is applied, it rounds up to 11. If the number is 10.9, adding 0.4 becomes 11.3, which rounds to 11.

例如,数字 10.1 然后变成 10.5。当应用round函数时,它向上取整到11。如果数字是10.9,加上0.4变成11.3,它四舍五入到11。

回答by user7425513

Excellent answer 'alextansc'. This little public function works a treat:

优秀的答案“alextansc”。这个小小的公共功能很有用:

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

回答by Stephanie C.

this works great as well

这也很好用

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

End Function

结束函数

Example roundup(10.333,2)=10.34

示例综合(10.333,2)=10.34

回答by Robert S.

Here is one that is simple and easy to understand:

这是一个简单易懂的:

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

NOTE: Do not forget to check your value(s) for null before calling the function! This function will roundup the smallest to the largest decimal point to the next whole integer regardless!

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

回答by MikeJ

Example of the whole number round up:

整数向上舍入的示例:

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

回答by benmakp benmakn

all you need to do is to use round(field+0.004999,2) or round(field+0.000499,3) ...

您需要做的就是使用 round(field+0.004999,2) 或 round(field+0.000499,3) ...