Created
September 15, 2016 18:48
-
-
Save paxperscientiam/ec5f61200e7393f3f1e18f6fd8a544de to your computer and use it in GitHub Desktop.
VBA 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