Hi,

When I run it

Dim A As Single
A = 12.345
Debug.Print Round(12.345, 2)
Debug.Print Round(A, 2)

I got different results:
12.34
12.35

Can you tell me why?

Recommended Answers

All 6 Replies

In the first case, 12.345 is probably being decoded as a double. In the second case, the variable A is a float. Welcome to the world of floating point numbers and rounding errors!

Thanks a lot. I tried this to show if A and 12.345 is the same:

Dim A As Single
A = 12.345
Debug.Print A = 12.345
Debug.Print Round(12.345, 2)
Debug.Print Round(A, 2)

and got output:
True
12.34
12.35

A quick check in vb.net and vbScript (sorry, do not have vb6 installed) gives the following results

Math.Round(1.5) is 2.0
Math.Round(2.5) is 2.0
Math.Round(3.5) is 4.0    <edit> corrected from 3.0 </edit>
Math.Round(4.5) is 4.0    <edit> corrected from 3.0 </edit>

If the integer portion is odd then it rounds up. If even then it rounds down. Personally I think this is brain dead and as a result I always do my own rounding.

Reverend Jim,
I also check it in VB.Net and found you might got mistake. What I got are
Debug.Print(Math.Round(1.5)) 'is 2
Debug.Print(Math.Round(2.5)) 'is 2
Debug.Print(Math.Round(3.5)) 'is 4
Debug.Print(Math.Round(4.5)) 'is 4

rubberman,

OK, in Vb.Net it looks better:

Debug.Print(Math.Round(1.5)) 'is 2
Debug.Print(Math.Round(2.5)) 'is 2
Debug.Print(Math.Round(3.5)) 'is 4
Debug.Print(Math.Round(4.5)) 'is 4

Dim A As Single
A = 12.345
Debug.Print (A = 12.345) 'is False! Perhaps A is something like 12.345000001
Debug.Print (A) 'is 12.345 but perhaps it is something like 12.345000001
Debug.Print (Math.Round(12.345, 2)) 'is 12.34
Debug.Print (Math.Round(A, 2)) 'is 12.35

DOH! You are right. I had a typo which I have corrected.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.