BigInteger.Parse Method (String)

Converts the string representation of a number to its BigInteger equivalent.

Namespace:  System.Numerics
Assembly:  System.Numerics (in System.Numerics.dll)

'Declaration
Public Shared Function Parse ( _
	value As String _
) As BigInteger

Parameters

value
Type: System.String
A string that contains the number to convert.

Return Value

Type: System.Numerics.BigInteger
A value that is equivalent to the number specified in the value parameter.

ExceptionCondition
ArgumentNullException

value is Nothing.

FormatException

value is not in the correct format.

The value parameter should be the string representation of a number in the following form.

[ws][sign]digits[ws]

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element

Description

ws

Optional white space.

sign

An optional sign. Valid sign characters are determined by the NumberFormatInfo.NegativeSign and NumberFormatInfo.PositiveSign properties of the current culture.

digits

A sequence of digits ranging from 0 to 9. Any leading zeros are ignored.

NoteNote

The string specified by the value parameter is interpreted by using the NumberStyles.Integer style. It cannot contain any group separators or decimal separator, and it cannot have a decimal portion.

The value parameter is parsed by using the formatting information in a System.Globalization.NumberFormatInfo object that is initialized for the current system culture. For more information, see NumberFormatInfo.CurrentInfo. To parse a string by using the formatting information of a specific culture, use the Parse(String, IFormatProvider) method.

Important noteImportant

If you use the Parse method to round-trip the string representation of a BigInteger value that was output by the ToString method, you should use the BigInteger.ToString(String) method with the "R" format specifier to generate the string representation of the BigInteger value. Otherwise, the string representation of the BigInteger preserves only the 50 most significant digits of the original value, and data may be lost when you use the Parse method to restore the BigInteger value.

The following example uses the Parse(String) method to instantiate two BigInteger objects. It multiplies each object by another number and then calls the Compare method to determine the relationship between the two values.


Dim stringToParse As String = String.Empty
Try
   ' Parse two strings.
   Dim string1, string2 As String
   string1 = "12347534159895123"
   string2 = "987654321357159852"
   stringToParse = string1
   Dim number1 As BigInteger = BigInteger.Parse(stringToParse)
   Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number1)
   stringToParse = string2
   Dim number2 As BigInteger = BigInteger.Parse(stringToParse)
   Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number2)
   ' Perform arithmetic operations on the two numbers.
   number1 *= 3
   number2 *= 2
   ' Compare the numbers.
   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      
Catch e As FormatException
   Console.WriteLine("Unable to parse {0}.", stringToParse)
End Try
' The example displays the following output:
'    Converted '12347534159895123' to 12,347,534,159,895,123.
'    Converted '987654321357159852' to 987,654,321,357,159,852.
'    1975308642714319704 is greater than 37042602479685369.      


.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft