Error with int() calculation

DilemmaDave

New Member
Joined
Apr 6, 2012
Messages
7
Hi All,

This is driving me batty! The int() function returns obviously wrong results and I can't figure out why.

I've simplified things with the following bit of code:

Sub Int_Error()
x = 39.8
y = x * 100
z = Int(y)
End Sub

If you step through this and watch the values , 'z' evaluates as 3979, instead of the correct value of 3980.

I've tested this on two different systems and have the same issue. I'm using Excel 2003, SP3.

z=Int(3980) returns the correct value, so it seems it only happens when the argument is passed as a variable.

This happens consistently with some specific values, 39.8 being one of those values.

Thoughts? Ideas?

Cheers,
David W.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Declare your variables, such that x is double, and y and Z are long. Z in fact then becomes redundant.
 
Upvote 0
Declare your variables, such that x is double, and y and Z are long. Z in fact then becomes redundant.

The actual expression I am calculating is:

HoleTo = Int(Cells(c.Row, 3) * 100)

When Cells(c.Row, 3) * 100 = 3980, int() returns 3979

Declaring HoleTo as Long does not resolve it.

Thanks
 
Upvote 0
Dont knows if z is necessary to you..
If not then Jon's tip is simple.

Declare x as double and y as long

It will automatically produce correct answer.
 
Upvote 0
You should always qualify the property you wish to use. In this case it is using the default range Value property, but you ought to be using Value2.

Code:
 Cells(c.Row, 3).Value2 * 100
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,384
Members
448,956
Latest member
JPav

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