Hi
Is there a function to reverse an integer?
For example ReverseInt(24) would return -24 or ReverseInt(-10) would return 10.
Thanks.
Printable View
Hi
Is there a function to reverse an integer?
For example ReverseInt(24) would return -24 or ReverseInt(-10) would return 10.
Thanks.
See if this helps,
Code:Private Function ReverseInt(Value As Integer) As Integer
If Value < 0 Then
ReverseInt = Abs(Value)
Else
ReverseInt = -Value
End If
End Function
I've never heard of that as the 'reverse' of a number, the 'negative' is the usual way to describe it.
The way to do it is to simply use a - sign, eg:
Code:MyVariable = 24 'set initial value
MyVariable = -MyVariable '"reverse" it
edit: hmm.. it seems I was quite slow, but at least I provided a different (and in this case simpler) solution!
I've always used the ABS() command hehe, so what's it good for then?
The Abs function returns the absolute (always positive) version of the number, whether it was already positive or not (so -10 and 10 would both give 10).
When/how that would be useful depends on what you are doing.. just like getting the negative of a number (as we did here) is.
Oh ya thats right, I need more coffee.Quote:
Originally Posted by si_the_geek
Si's solution is obviously the best, but this appeals to my warped mind (only applies to whole numbers) :D
Code:intI = Not intI -1