Mega Code Archive

 
Categories / VB.Net / Data Types
 

Call TryParse with the default value of style and a provider supporting the tilde as negative sign

Imports System.Numerics Imports System Imports System.Globalization Module Example    Public Sub Main()         Dim numericString As String         Dim number As BigInteger = BigInteger.Zero         numericString = "  -300   "         If BigInteger.TryParse(numericString, NumberStyles.Integer,                                New BigIntegerFormatProvider(), number) Then            Console.WriteLine("'{0}' was converted to {1}.",                              numericString, number)                                      Else            Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",                              numericString)         End If                                                  End Sub End Module Public Class BigIntegerFormatProvider : Implements IFormatProvider     Public Function GetFormat(ByVal formatType As Type) As Object Implements IFormatProvider.GetFormat         If formatType Is GetType(NumberFormatInfo) Then             Dim numberFormat As New NumberFormatInfo             numberFormat.NegativeSign = "~"             Return numberFormat         Else             Return Nothing         End If     End Function End Class