ASP / VBScript-Int()与CInt()

时间:2020-03-05 18:41:41  来源:igfitidea点击:

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处溢出。

回答

这是另一个区别:

脚本:

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

结果:

4091
4090   (????)
4091

有什么想法吗?

回答

解决此问题的常用方法是手动强制进行四舍五入。此问题与FORTRAN一样古老。

代替

a = int(40.91 * 100)

使用

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

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