Int function rounds down the integer 2 down to 1

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,558
I don't understand why the Int function rounds down the value of 2 down to 1 in the second calculation?

Code:
[COLOR=darkblue]Sub[/COLOR] Foo()
    
    [COLOR=green]'Correct result = 2[/COLOR]
    Debug.Print (0.15 - 0.07) / 0.04
    
    [COLOR=green]'Why does Int round down the result of 2 to 1 ?[/COLOR]
[COLOR=#ff0000]    Debug.Print Int((0.15 - 0.07) / 0.04)[/COLOR]
    
    [COLOR=green]'Yet this is correct = 2[/COLOR]
    Debug.Print [COLOR=darkblue]CInt[/COLOR]((0.15 - 0.07) / 0.04)
    
    [COLOR=green]'And this is correct = 2[/COLOR]
    Debug.Print Int(0.08 / 0.04)
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try:

Code:
Debug.Print (0.15 - 0.07) = 0.08

and you'll get False, and:

Code:
Debug.Print (0.15 - 0.07) < 0.08

returns True, which is why Int is rounding down.

Also remember that CInt and Int are very different things! CInt(1.5) returns 2
 
Last edited:
Upvote 0
You are welcome.

Every now and then that issue raises it ugly head and can be confounding (as it doesn't seem to follow logic!)
I usually find the ROUND function to be my best friends in those cases.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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