加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

vb.net – Integer.Parse与CInt

发布时间:2020-12-16 23:53:49 所属栏目:大数据 来源:网络整理
导读:基本上,在大多数日常的编程任务中,我一直使用Integer.Parse和 CInt,但是我有点混淆了两者之间的区别。 Integer.Parse和CInt在VB.NET中有什么区别? CInt比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。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读