views:

4559

answers:

3

What is the difference in ASP/VBScript between Int() & CInt()?

+6  A: 

The Int function returns the integer part of a specified number.

The CInt function converts an expression to type Integer.

And the best answer comes from MSDN

CInt differs from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. When the fractional part is exactly 0.5, the CInt function always rounds it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2.

Pascal Paradis
+3  A: 

And, the most important difference (IME, at least)....is that CInt overflows at 32,767.

Mark Brackett
+1  A: 

Here is another difference:

Script:

wscript.echo 40.91 * 100
wscript.echo Int(40.91 * 100)
wscript.echo CInt(40.91 * 100)

result:

4091
4090   (????)
4091

Any thoughts?

Floating point rounding. The actual value of "40.91" in the code is extremely close to but just less than the theoretical decimal value. CInt rounds to nearest, Int truncates.
Thom Smith