Created
November 5, 2015 02:47
-
-
Save paxperscientiam/7c833399c17a97d8e145 to your computer and use it in GitHub Desktop.
An Excel VBA UDF to round a decimal number to the nearest integer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function getRoundInteger(num As Double) As Double | |
Const BUG As Boolean = 1 | |
Dim er As Integer: er = 0 | |
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' | |
' getRoundInteger( ) v.1.0 | |
' Function getRoundInteger( ) takes a double and uses the "rounds half up" method (towards +∞) to get the nearest "integer". | |
' The error stuff was just an experiment that will get it's on follow up. | |
' Works pretty well, though use at your own discretion! | |
' ~ Pax Per Scientiam | |
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' | |
On Error GoTo errorhandler: | |
If BUG Then er = er + 1: Debug.Print "[" & er & "] " & "getRoundInteger:$ Will round " & num & " to the nearest integer." | |
getRoundInteger = (-1) * (Int(num) + 1) * ((Abs(num - Int(num)) >= 0.5)) - Int(num) * ((Abs(num - Int(num)) < 0.5)) | |
If BUG Then er = er + 1: Debug.Print "[" & er & "] " & "getRoundInteger:$ Rounding " & num & " yields " & getRoundInteger | |
Exit Function | |
errorhandler: | |
If BUG Then er = er + 1: Debug.Print "[" & er & "] " & "getRoundInteger:$ Error # " & Err & " on line " & Erl & " : " & Error(Err) | |
getRoundInteger = CVErr(2015) | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment