vb.net Integer.Parse 与 CInt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/423820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integer.Parse vs. CInt
提问by KG Sosa
Basically, I have been using both Integer.Parse
and CIntin most of my daily programming tasks, but I'm a little bit confused of what the difference is between the two.
基本上,我在大多数日常编程任务中都同时使用Integer.Parse
和CInt,但我对两者之间的区别有点困惑。
What is the difference between Integer.Parse
and CInt
in VB.NET?
Integer.Parse
和CInt
在 VB.NET 中有什么区别?
回答by Binary Worrier
CInt
does a whole lot more than Integer.Parse
.
CInt
做的远远不止Integer.Parse
.
CInt
will first check to see if what it was passed is an integer, and then simply casts it and returns it. If it's a double it will try to convert it without first converting the double to a string.
CInt
将首先检查它传递的是否是一个整数,然后简单地转换它并返回它。如果它是双精度数,它将尝试转换它而不先将双精度数转换为字符串。
See this from the help for CInt
and other Type Conversion Functions
从CInt
其他类型转换函数的帮助中查看此内容
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.
分数部分。将非整数值转换为整数类型时,整数转换函数(CByte、CInt、CLng、CSByte、CShort、CUInt、CULng 和 CUShort)会删除小数部分并将值四舍五入为最接近的整数。
如果小数部分正好是 0.5,则整数转换函数会将其四舍五入到最接近的偶数。例如,0.5 舍入为 0,1.5 和 2.5 均舍入为 2。这有时称为银行家舍入,其目的是补偿在将许多此类数字相加时可能累积的偏差。
So in short, it does much more than convert a string to an integer, e.g. applying specific rounding rules to fractions, short circuting unecessary conversions etc.
所以简而言之,它不仅仅是将字符串转换为整数,例如将特定的舍入规则应用于分数、短路不必要的转换等。
If what you're doing is converting a string to an integer, use Integer.Parse
(or Integer.TryParse
), if you're coercing an unknown value (e.g. a variant
or object
from a database) to an integer, use CInt
.
如果您正在做的是将字符串转换为整数,请使用Integer.Parse
(或Integer.TryParse
),如果您要将未知值(例如 avariant
或object
来自数据库)强制转换为整数,请使用CInt
.
回答by Drake
Looking with ILDASMat some sample code you can see that CInt is converted to this call:
与展望ILDASM一些示例代码,你可以看到,CINT转换这一号召:
Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToInteger(string)
Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToInteger(string)
Using .NET Reflector, you can extract this piece of code:
使用.NET Reflector,您可以提取这段代码:
Public Shared Function ToInteger(ByVal Value As String) As Integer
Dim num As Integer
If (Value Is Nothing) Then
Return 0
End If
Try
Dim num2 As Long
If Utils.IsHexOrOctValue(Value, (num2)) Then
Return CInt(num2)
End If
num = CInt(Math.Round(Conversions.ParseDouble(Value)))
Catch exception As FormatException
Throw New InvalidCastException(Utils.GetResourceString("InvalidCast_FromStringTo", New String() { Strings.Left(Value, &H20), "Integer" }), exception)
End Try
Return num
End Function
You can see that internally it calls Conversions.ParseDouble.
您可以看到它在内部调用Conversions.ParseDouble。
Therefore, as already explained by Binary Worrier, use Integer.Parse for string coercing and CInt only for casting.
因此,正如 Binary Worrier 已经解释过的,使用 Integer.Parse 进行字符串强制,而 CInt 仅用于转换。
回答by cmsjr
The <Data Type>.Parse
methods are used to extract a value of the type from a string that represents the value (e.g 2 from "2") The C<Data Type>
functions operate on expressions and return a variant sub-typed to the desired type (e.g. CInt("2")
OR CInt(SomeDouble + SomeDouble)
, etc.).
这些<Data Type>.Parse
方法用于从表示值的字符串中提取该类型的值(例如,“2”中的 2)。C<Data Type>
函数对表达式进行操作并返回子类型化为所需类型的变体(例如CInt("2")
ORCInt(SomeDouble + SomeDouble)
等)。
回答by simon leclercq
Here is a real difference : Integer.parse("1.00") will thrown an error. CInt("1.00") will work
这是一个真正的区别:Integer.parse("1.00") 会抛出一个错误。CInt("1.00") 会起作用