Integer.Parse和CInt在VB.NET中有什么区别?
Cint将先检查一下它传递的是一个整数,然后简单地将其转换并返回。如果它是一个双倍,它将尝试转换它,而无需首先将double转换为字符串。
从CInt和其他Type Conversion Functions的帮助中查看
Fractional Parts. When you convert a
nonintegral value to an integral type,
the integer conversion functions
(CByte,CInt,CLng,CSByte,CShort,
CUInt,CULng,and CUShort) remove the
fractional part and round the value to
the closest integer.If the fractional part is exactly 0.5,
the integer conversion functions round
it to the nearest even integer. For
example,0.5 rounds to 0,and 1.5 and
2.5 both round to 2. This is sometimes called banker’s rounding,and its
purpose is to compensate for a bias
that could accumulate when adding many
such numbers together.
所以简而言之,它不仅仅是将一个字符串转换为一个整数,对分数应用特定的舍入规则,缩短不必要的转换等等。
如果您正在做的是将字符串转换为整数,请使用integer.parse(或integer.TryParse),如果您将一个未知值(例如数据库中的变体或对象)强制为整数,请使用CInt。