C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Info: After Integer.TryParse returns True, we have an actual Integer. We show this by dividing the Integer by two.
BooleanAnd: This would not be possible with a number stored in String format. A string cannot be divided.
VB.NET program that converts String to Integer
Module Module1
Sub Main()
' Input String.
Dim value As String = "123456"
' Use Integer.TryParse.
Dim i As Integer
If (Integer.TryParse(value, i)) Then
Console.WriteLine("Integer: {0}", i)
Console.WriteLine("Half: {0}", i / 2)
End If
End Sub
End Module
Output
Integer: 123456
Half: 61728
Also: You do not have to write custom algorithms. TryParse will tell you if you have an invalid number.
Therefore: Integer.TryParse is probably best unless you have serious performance demands.