My test case can be downloaded from http://home.comcast.net/~jasmith4/Ex...g/RoundBug.xls
It's an Excel 2003 file, but the bug shows up in Excel 2007 and 2010 too.
The bug has to do with VBA rounding numbers that end with one decimal digit, xxx.5 -- for example, if you round 0.5 you'll get 0, not 1, but if you round 1.5 you'll get 2! Here are a few more results using VBA's ROUND function -- yikes!
0.5 0
1.5 2
2.5 2
3.5 4
4.5 4
5.5 6
6.5 6
7.5 8
8.5 8
9.5 10
So I decided to do a little more testing, and here's how I composed the test case:
First I created a new XLS (Excel 2003) file, added a regular module to its VBAproject, and wrote the following code into it:
Then in the UI's first tab I added 12 headings to the first row (A1:L1):Please Login or Register to view this content.
Test number --> A1
UI Round(#,0) --> A2, etc.
UI Int(#)
UI Fixed(#,0,True)
UI RoundUp(#,0)
UI RoundDown(#,0)
VBA Round(#,0)
VBA implicit conversion
VBA CLng(#)
VBA CInt(#)
VBA Int(#)
VBA Fix(#)
Next I added the following formulas to the second row (A2:L2):
-50.5 <-- this is my first test number, in A2
=ROUND($A2,0) --> A3
=INT($A2) --> A4, etc.
=VALUE(FIXED($A2,0,TRUE))
=ROUNDUP($A2,0)
=ROUNDDOWN($A2,0)
=RoundVBA($A2)
=DoubleToInt($A2)
=RoundClng($A2)
=RoundCint($A2)
=RoundInt($A2)
=RoundFix($A2)
Finally I entered -49.5 into cell A3, selected A2:A3, filled down to A103 (ending up with 50.5), and filled down all the formulas.
The results are startling!!
Bookmarks