vb.net – Integer.Parse与CInt

基本上,在大多数日常的编程任务中,我一直使用Integer.Parse和 CInt,但是我有点混淆了两者之间的区别。

Integer.Parse和CInt在VB.NET中有什么区别?

CInt比integer.Parse要好很多。

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。

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...