Visual Basic 6 Round Error

I just hit an interesting error in VB6 today – it’s best described as a Visual Basic 6 round error.

I was trying to calculate a distance between two points. No problem there except I didn’t want to put something on a page that said “48.723569” miles. That would look dumb.

No problem, I thought, I’ll format the output using VB’s format command, giving the number string as “#####0.0”.

But for certain numbers, format in VB 6 kept giving me the answer 0.0. Not exactly what I was looking for…

No problem, I’ll use Round instead.

Same error. Some calculations worked, others got rounded to zero regardless…

Overcoming the VB 6 round error:

Debugging showed that the number was too long to display properly (pi is involved in the calculation, so in theory the number goes on forever).

Instead VB’s debug window displayed the start of the number but ended it in “E-02”

Which is was what confusing Format and Round.

My working (but probably not elegant) solution was to cstr the number, remove the E-02 part and then carry on:

SortedDistance = CStr(myDistance)
SortedDistance = Replace(SortedDistance, “E-02”, “”)

Two simple lines that got rid of the headache and get rid of the Visual Basic 6 round error.

And if you’re struggling with other VB6 stuff then check this out.

Leave a Reply

Your email address will not be published. Required fields are marked *