2015年3月6日 星期五

Access Round Function and custom Round Function

This tutorial explains how to use Access Round Function (round to even logic) and create a custom Round Function to simulate Excel Round Function.


Access Round Function – Round to even logic


Access Round Function is completely different from Excel Round Function.


For Excel Found Function, if decimal part is >=0.5, the integer rounds up, otherwise the integer rounds down.


For Access Round Function, as well as Access VBA, Excel VBA, the Round Function uses “Round to even” logic. Integer rounds up if decimal part >=0.6, round down if <=0.4. For case decimal part exactly =0.5, , then round down, otherwise round up(round up to even integer), same logic applies to negative number (See example).


I summarize the Access round to even logic below.


If decimal part >=0.6 Then
   Round up
ElseIf decimal part <=0.4 Then
   Round down
ElseIf decimal part = 0.5 Then
   If integer part is even number Then
        Round down
   ElseIf integer part is odd number Then
        Round up
  End If
End If

Round to even logic has other names:


-unbiased rounding


-convergent rounding


-statistician’s rounding


-Dutch rounding


-Gaussian rounding


-odd-even rounding


-bankers’ rounding


-broken rounding


Syntax of Access Round Function – Round to even logic


Round ( expression, [ decimal_places ] )

If [decimal_places] is omitted, default is 0 (round to integer)


Example of Access Round Function – Round to even logic


Round([Number],0)
















































































































































































NumberAccess RoundExcel RoundCheck Diff
0.100
0.200
0.300
0.400
0.501integer part is even number, round down
0.611
0.711
0.811
0.911
111
1.111
1.211
1.311
1.411
1.522integer part is odd number, round up
1.622
1.722
1.822
1.922
222
2.122
2.222
2.322
2.422
2.523integer part is even number, round down
2.633
2.733
2.833
2.933
333
3.133
3.233
3.333
3.433
3.544integer part is odd number, round up
3.644
3.744
3.844
3.944
444
4.144
4.244
4.344
4.444
-4.545integer part is even number, round down
-4.655
-4.755
-4.855
-4.955
-555
-5.155
-5.255
-5.355
-5.455
-5.566integer part is odd number, round up

VBA Code of custom Access Round Function – simulate Excel Round function


Function wRound(pValue As Double, digit) As Double
    Dim ExpandedValue
    Dim IntPart As Long
    Dim FractionPart As Double
    Dim negNumber As Boolean
    If pValue < 0 Then
        pValue = 0 - pValue
        negNumber = True
    End If
    
    ExpandedValue = pValue * (10 ^ digit) 'Retrieve integer part of the number
    IntPart = Int(ExpandedValue)
    FractionPart = ExpandedValue - IntPart 'Retrieve the fraction part of the number
    If FractionPart < 0.5 Then
        result = IntPart / 10 ^ digit
    ElseIf FractionPart >= 0.5 Then
        result = (IntPart + 1) / 10 ^ digit
    End If
    
    If negNumber = True Then
        wRound = 0 - result
    Else
        wRound = result
    End If
End Function

Syntax of custom Access Round Function – simulate Excel Round function


wRound(pValue, digit)





pValuethe decimal value you want to convert
digitthe number of decimal places you want to convert to

Outbound References


http://www.techonthenet.com/access/functions/numeric/round.php



Access Round Function and custom Round Function

沒有留言:

張貼留言