关于ASP Classic:ASP / VBScript-Int()vs CInt()

关于ASP Classic:ASP / VBScript-Int()vs CInt()

ASP/VBScript - Int() vs CInt()

Int()CInt()在ASP / VBScript中有什么区别?


  • Int()

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

  • CInt()

The CInt function converts an expression to type Integer.

最好的答案来自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.


而且,最重要的区别(至少是IME)是CInt在32,767处溢出。


通常,此问题的答案是手动强制重新取整。 此问题与FORTRAN一样古老。

代替

1
a = int(40.91 * 100)

采用

1
2
b = 40.91 * 100
a = int(b + 0.5)

非常老套的技巧,有时在Excel电子表格中仍然有用。


这是另一个区别:

脚本:

1
2
3
wscript.echo 40.91 * 100
wscript.echo Int(40.91 * 100)
wscript.echo CInt(40.91 * 100)

结果:

1
2
3
4091
4090   (????)
4091

有什么想法吗?


推荐阅读