When we convert a float to integer in visual basic 6.0, how does it round off the fractional part? I am talkin about the automatic type conversion.
If we assign like
Dim i as Integer i=5.5 msgbox i
What will it print? 5 or 6 ??
I was getting ‘5’ a couple of months before. One day it started giving me 6! Any idea whats goin wrong? Did microsoft released some patches to fix something?
Update : 5.5 gets converted to 6 but 8.5 to 8 !
Update 2 : Adding CInt makes no difference. CInt(5.5) gives 6 and Cint(8.5) gives 8!! Kinda weired behaviour. I should try something like floor(x + 0.49);
Part of this is in the VB6 help: topic Type Conversion Functions. Unfortunately it’s one of the topics that’s not in the VB6 documentation on the MSDN website, but if you’ve installed the help with VB6, it will be there.
Implicit type coercion – a.k.a. "evil type coercion (PDF)" – from a floating point number to a whole number uses the same rounding rules as CInt and CLng. This behaviour doesn’t seem to be documented anywhere in the manual.
If you want to round up when the fractional part is >= 0.5, and down otherwise, a simple way to do it is
And off the top of my head, here’s my briefer version of Mike Spross’s function which is a replacement for the VB6 Round function.
Sample output: