本文整理汇总了VB.NET中System.Numerics.BigInteger.TryParse方法的典型用法代码示例。如果您正苦于以下问题:VB.NET BigInteger.TryParse方法的具体用法?VB.NET BigInteger.TryParse怎么用?VB.NET BigInteger.TryParse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Numerics.BigInteger
的用法示例。
在下文中一共展示了BigInteger.TryParse方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1:
Dim number1 As BigInteger = BigInteger.Zero
Dim number2 As BigInteger = BigInteger.Zero
Dim succeeded1 As Boolean = BigInteger.TryParse("-12347534159895123", number1)
Dim succeeded2 As Boolean = BigInteger.TryParse("987654321357159852", number2)
If succeeded1 AndAlso succeeded2
number1 *= 3
number2 *= 2
Select Case BigInteger.Compare(number1, number2)
Case -1
Console.WriteLine("{0} is greater than {1}.", number2, number1)
Case 0
Console.WriteLine("{0} is equal to {1}.", number1, number2)
Case 1
Console.WriteLine("{0} is greater than {1}.", number1, number2)
End Select
Else
If Not succeeded1 Then
Console.WriteLine("Unable to initialize the first BigInteger value.")
End If
If Not succeeded2 Then
Console.WriteLine("Unable to initialize the second BigInteger value.")
End If
End If
输出:
1975308642714319704 is greater than -37042602479685369.
示例2: BigIntegerFormatProvider
Dim numericString As String
Dim number As BigInteger = BigInteger.Zero
' Call TryParse with default values of style and provider.
numericString = " -300 "
If BigInteger.TryParse(numericString, NumberStyles.Integer,
Nothing, number) Then
Console.WriteLine("'{0}' was converted to {1}.",
numericString, number)
Else
Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
numericString)
End If
' Call TryParse with the default value of style and
' a provider supporting the tilde as negative sign.
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
' Call TryParse with only AllowLeadingWhite and AllowTrailingWhite.
' Method returns false because of presence of negative sign.
numericString = " -500 "
If BigInteger.TryParse(numericString,
NumberStyles.AllowLeadingWhite Or NumberStyles.AllowTrailingWhite,
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
' Call TryParse with AllowHexSpecifier and a hex value.
numericString = "F14237FFAAC086455192"
If BigInteger.TryParse(numericString,
NumberStyles.AllowHexSpecifier,
Nothing, number) Then
Console.WriteLine("'{0}' was converted to {1} (0x{1:x}).",
numericString, number)
Else
Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
numericString)
End If
' Call TryParse with AllowHexSpecifier and a negative hex value.
' Conversion fails because of presence of negative sign.
numericString = "-3af"
If BigInteger.TryParse(numericString, NumberStyles.AllowHexSpecifier,
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
' Call TryParse with only NumberStyles.None.
' Conversion fails because of presence of white space and sign.
numericString = " -300 "
If BigInteger.TryParse(numericString, NumberStyles.None,
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
' Call TryParse with NumberStyles.Any and a provider for the fr-FR culture.
' Conversion fails because the string is formatted for the en-US culture.
numericString = "9,031,425,666,123,546.00"
If BigInteger.TryParse(numericString, NumberStyles.Any,
New CultureInfo("fr-FR"), number) Then
Console.WriteLine("'{0}' was converted to {1}.",
numericString, number)
Else
Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
numericString)
End If
' Call TryParse with NumberStyles.Any and a provider for the fr-FR culture.
' Conversion succeeds because the string is properly formatted
' For the fr-FR culture.
numericString = "9 031 425 666 123 546,00"
If BigInteger.TryParse(numericString, NumberStyles.Any,
New CultureInfo("fr-FR"), number) Then
Console.WriteLine("'{0}' was converted to {1}.",
numericString, number)
Else
Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
numericString)
End If
输出:
-300 ' was converted to -300. Conversion of ' -300 ' to a BigInteger failed. Conversion of ' -500 ' to a BigInteger failed. F14237FFAAC086455192' was converted to -69613977002644837412462 (0xf14237ffaac086455192). Conversion of '-3af' to a BigInteger failed. Conversion of ' -300 ' to a BigInteger failed. Conversion of '9,031,425,666,123,546.00' to a BigInteger failed. 9 031 425 666 123 546,00' was converted to 9031425666123546.
示例3: GetFormat
Public Class BigIntegerFormatProvider : Implements IFormatProvider
Public Function GetFormat(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
示例4: Example
' 导入命名空间
Imports System.Globalization
Imports System.Numerics
Module Example
Public Sub Main()
Dim hexStrings() As String = { "80", "E293", "F9A2FF", "FFFFFFFF",
"080", "0E293", "0F9A2FF", "0FFFFFFFF",
"0080", "00E293", "00F9A2FF", "00FFFFFFFF" }
Dim number As BigInteger = BigInteger.Zero
For Each hexString As String In hexStrings
If BigInteger.TryParse(hexString, NumberStyles.AllowHexSpecifier,
Nothing, number) Then
Console.WriteLine("Converted 0x{0} to {1}.", hexString, number)
Else
Console.WriteLine("Cannot convert '{0}' to a BigInteger.", hexString)
End If
Next
End Sub
End Module
输出:
Converted 0x80 to -128. Converted 0xE293 to -7533. Converted 0xF9A2FF to -417025. Converted 0xFFFFFFFF to -1. Converted 0x080 to 128. Converted 0x0E293 to 58003. Converted 0x0F9A2FF to 16360191. Converted 0x0FFFFFFFF to 4294967295. Converted 0x0080 to 128. Converted 0x00E293 to 58003. Converted 0x00F9A2FF to 16360191. Converted 0x00FFFFFFFF to 4294967295.