Export (0) Print
Expand All

BigInteger.TryParse Method (String, BigInteger%)

Tries to convert the string representation of a number to its BigInteger equivalent, and returns a value that indicates whether the conversion succeeded.

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

public static bool TryParse(
	string value,
	out BigInteger result
)

Parameters

value
Type: System.String
The string representation of a number.
result
Type: System.Numerics.BigInteger%
When this method returns, contains the BigInteger equivalent to the number that is contained in value, or zero (0) if the conversion fails. The conversion fails if the value parameter is null or is not of the correct format. This parameter is passed uninitialized.

Return Value

Type: System.Boolean
true if value was converted successfully; otherwise, false.

ExceptionCondition
ArgumentNullException

value is null.

The TryParse(String, BigInteger) method is like the Parse(String) method, except that it does not throw an exception if the conversion fails. This method eliminates the need to use exception handling to test for a FormatException if value is invalid and cannot be successfully parsed.

The value parameter should be the string representation of a decimal 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 decimal digits ranging from 0 to 9.

NoteNote

The string specified by the value parameter cannot contain any group separators or decimal separator, and it cannot have a decimal portion.

The value parameter is interpreted by using the NumberStyles.Integer style. In addition to the decimal digits, only leading and trailing spaces with a leading sign are allowed. To explicitly define the style elements with the culture-specific formatting information that can be present in value, call the TryParse(String, NumberStyles, IFormatProvider, BigInteger) method.

The value parameter is parsed by using the formatting information in a NumberFormatInfo object for the current culture. For more information, see NumberFormatInfo.CurrentInfo.

This overload interprets all digits in the value parameter as decimal digits. To parse the string representation of a hexadecimal number, call the TryParse(String, NumberStyles, IFormatProvider, BigInteger) overload instead.

The following example uses the TryParse(String, BigInteger) method to instantiate two BigInteger objects. If the conversions succeed, it multiplies each object by another number and then calls the Compare method to determine the relationship between the two objects.


BigInteger number1, number2;
bool succeeded1 = BigInteger.TryParse("-12347534159895123", out number1);
bool succeeded2 = BigInteger.TryParse("987654321357159852", out number2);
if (succeeded1 && succeeded2)
{
   number1 *= 3;
   number2 *= 2;
   switch (BigInteger.Compare(number1, number2))
   {
      case -1:
         Console.WriteLine("{0} is greater than {1}.", number2, number1);
         break;
      case 0:
         Console.WriteLine("{0} is equal to {1}.", number1, number2);
         break;
      case 1:
         Console.WriteLine("{0} is greater than {1}.", number1, number2);
         break;
   }      
}
else
{
   if (! succeeded1) 
      Console.WriteLine("Unable to initialize the first BigInteger value.");

   if (! succeeded2)
      Console.WriteLine("Unable to initialize the second BigInteger value.");
}
// The example displays the following output:
//      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.

Community Additions

ADD
Show:
© 2014 Microsoft