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.
 
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


Problem is, Some of the values that need to be evaluated have more than 3 decimals, I want to discard any beyond two, hence the use of int().

Edit: Just fully read that. My bad.
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Still evaluates to 3979.

Dim HoleTo as Long
HoleTo = Int(Cells(C.Row, 3).Value2 * 100)


Value in the cell referenced is 39.8
 
Upvote 0
Lose the int mate.

Code:
Dim HoleTo As Long
HoleTo = Cells(c.Row, 3).Value2 * 100

It will be truncated because Long Integers do not support decimals values.
 
Upvote 0
Lose the int mate.

Code:
Dim HoleTo As Long
HoleTo = Cells(c.Row, 3).Value2 * 100

It will be truncated because Long Integers do not support decimals values.


Yes, Of course. I was too hung up on the original problem.

Thank you so much.


It still concerns me that something as simple as the original I posted evaluates incorrectly. Is there a reason for that? It's pretty straight forward. How is int(3980) different from:

y=3980
z=int(y)

???
 
Upvote 0
Open a new workbook and enter 39.8 in A1
In any other cell enter =LEN(MOD(A1,1))-2.

This should result in 1 because you entered a number with a single decimal space, yet the answer you get is ???

Why? Well the answer to that is not so straight forward to explain, and not so easy to understand. All I'm going to do is refer you to this article. :)
 
Upvote 0
Thank You Jon.

Interesting read.

Now I'm going to be questioning the validity of all my calculations. :(

Ah well, at least this problem is solved and it explains some other issues I have had in the past. With your helpful guidance at least I'll have some insight in the future.

Thanks again,
David W.
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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