问题
For Access 2013, I need a way to round up any fractional numbers to the next whole number in an SQL query.
Example:
SELECT ROUNDUP(NumberValues) FROM Table1
In the above query, a row with 1.25 should return as 2 after querying.
As far as I know, there's no ROUNDUP function in Access 2013 for use in a SQL query statement.
回答1:
I found a ROUNDUP equivalent from this link: 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
The syntax is counter-intuitive, but it works exactly as I intended.
回答2:
I have found the easiest way to round up a number in access is to use the round function like this:
Round([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.
回答3:
Excellent answer 'alextansc'. This little public function works a treat:
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
回答4:
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
回答5:
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!
来源:https://stackoverflow.com/questions/31980106/q-how-to-roundup-a-number-in-access-2013