代码之家  ›  专栏  ›  技术社区  ›  Seibar

asp/vbscript-int()与cint()。

  •  21
  • Seibar  · 技术社区  · 17 年前

    ASP/VBScript的区别在于 Int() CInt() ?

    4 回复  |  直到 8 年前
        1
  •  33
  •   Pascal Paradis    17 年前

    int函数返回指定数字的整数部分。

    cint函数将表达式转换为integer类型。

    最好的答案来自 MSDN

    cint与fix和int函数不同,fix和int函数截断数字的小数部分,而不是取整。当小数部分正好为0.5时,CINT函数总是将其舍入到最接近的偶数。例如,0.5转为0,1.5转为2。

        2
  •  16
  •   Mark Brackett    17 年前

    最重要的区别(至少是IME)是CINT overflows at 32,767 .

        3
  •  3
  •   Vlado    17 年前

    另一个区别是:

    脚本:

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

    结果:

    4091
    4090   (????)
    4091
    

    有什么想法吗?

        4
  •  3
  •   Peter O. Manuel Pinto    13 年前

    这个问题通常的答案是手动强制重新舍入。这个问题和Fortran一样古老。

    而不是

    a = int(40.91 * 100)
    

    使用

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

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

    推荐文章