Int function rounds down the integer 2 down to 1

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,489
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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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,216,113
Messages
6,128,905
Members
449,477
Latest member
panjongshing

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