Int16.TryParse(String, NumberStyles, IFormatProvider, Int16%) method with a number of different string values. : Integer Parser « Data Types « VB.Net






Int16.TryParse(String, NumberStyles, IFormatProvider, Int16%) method with a number of different string values.

  

Imports System.Globalization

Module MainClass
   Public Sub Main()
      Dim numericString As String
      Dim styles As NumberStyles

      numericString = "1"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)


      numericString = "-30677"
      styles = NumberStyles.None
      CallTryParse(numericString, styles)

      numericString = "1.00"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)

      numericString = "1.72"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)

      numericString = "2,593" 
      styles = NumberStyles.Integer Or NumberStyles.AllowThousands
      CallTryParse(numericString, styles)

      numericString = "11232E-01"
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(numericString, styles) 

      numericString = "12E01233"
      CallTryParse(numericString, styles) 

      numericString = "81230c1"
      CallTryParse(numericString, NumberStyles.HexNumber)

      numericString = "0x80C1"
      CallTryParse(numericString, NumberStyles.HexNumber)

   End Sub

   Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
      Dim number As Short
      Dim result As Boolean = Int16.TryParse(stringToConvert, styles,CultureInfo.InvariantCulture, number)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
      Else
         Console.WriteLine("Attempted conversion of '{0}' failed.",Convert.ToString(stringToConvert))
      End If                                                                           
   End Sub
End Module

   
    
  








Related examples in the same category

1.Validate an IntegerValidate an Integer