Int() bug?

bmacr

Board Regular
Joined
Jan 14, 2004
Messages
94
Can anyone tell me why Int(2.8*10) returns 27, not 28?

I've tried it in A2000 and A2002 with same results. If you use 1.8, it works properly. If you use 2.8, 3.8, 4.8, 5.8, 6.8, or 7.8 it fails, but if you use 8.8 it works again. Also, if you try Int(2.8*100), it returns 279 - and so on.
I've found a workaround for my situation, but this seems like a rather glaring thing to not have been corrected by now. Am I right in calling this a bug?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I hvae just tried this in A97 and it works correctly there. Not much help to you though. You may beable to force the correct answer though by using Cint() which rounds rather than truncates, rather depends on what your data range.

HTH

Peter
 
Upvote 0
Hi,

It works OK in Access 2003. One thing, Int is part of the VBA library and the same one used in all other applications that support VBA. Do you experience the same problem in Excel, Word, etc?
 
Upvote 0
Thanks for your replies.
It seems to work properly if entered in a cell in Excel as a formula, but if I enter it in the VBE, it doesn't.
Try the following in the immediate window:
?Int(1.8*10)
?Int(2.8*10)

I get 18 and 27 respectively. I've tried it on 2 different machines.
 
Upvote 0
Curioulsy in debug I get 27.99999, in a control on a form I get 28! where are you using it?

peter
 
Upvote 0
This is not so much a bug as the way floating point numbers are handled. If you do a search on floating point, I believe that there are a number of threads on this topic. You can get around this sometimes by using the round function like:

?int(round(1.8*10,10))
?int(round(2.8*10,10))
 
Upvote 0
While I don't completely understand floating point arithmetic, it appears that that is the answer.
Peter, FYI I was using this in a VBA function to test for a second digit.
e.g.
If (x*10) <> Int(x*10) Then 'The second digit isn't zero

We'd been using this DB for several months when suddenly there was this small inaccuracy that I couldn't explain. Now you've explained it for me.
Thanks again!
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top