Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

BigInteger.Parse Method (String, IFormatProvider)

Converts the string representation of a number in a specified culture-specific format to its BigInteger equivalent.

Namespace:  System.Numerics
Assembly:  System.Numerics (in System.Numerics.dll)
public static BigInteger Parse(
	string value,
	IFormatProvider provider
)

Parameters

value
Type: System.String

A string that contains a number to convert.

provider
Type: System.IFormatProvider

An object that provides culture-specific formatting information about value.

Return Value

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

value is null.

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 NumberFormatInfo object that is returned by the provider object's GetFormat method.

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 using the NumberStyles.Integer style. It cannot contain any group separators or decimal separator, and it cannot have a decimal portion.

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 provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object that provides culture-specific formatting information. When the Parse(String, IFormatProvider) method is invoked, it calls the provider parameter's GetFormat method and passes it a Type object that represents the NumberFormatInfo type. The GetFormat method then returns the NumberFormatInfo object that provides information about the format of the value parameter. There are three ways to use the provider parameter to supply custom formatting information to the parse operation:

  • You can pass a CultureInfo object that represents the culture that supplies formatting information. Its GetFormat method returns the NumberFormatInfo object that provides numeric formatting information for that culture.

  • You can pass the actual NumberFormatInfo object that provides numeric formatting information. (Its implementation of GetFormat just returns itself.)

  • You can pass a custom object that implements IFormatProvider. Its GetFormat method instantiates and returns the NumberFormatInfo object that provides formatting information.

If provider is null, the formatting of value is interpreted based on the NumberFormatInfo object of the current culture.

The following examples show two ways to define the tilde (~) as a negative sign for formatting BigInteger values. Note that to display the BigInteger values in the same format as the original strings, your code must call the BigInteger.ToString(IFormatProvider) method and pass it the NumberFormatInfo object that provides formatting information.

The first example defines a class that implements IFormatProvider and uses the GetFormat method to return the NumberFormatInfo object that provides formatting information.

public class BigIntegerFormatProvider : IFormatProvider
{
   public object GetFormat(Type formatType) 
   {
      if (formatType == typeof(NumberFormatInfo)) 
      {
         NumberFormatInfo numberFormat = new NumberFormatInfo();
         numberFormat.NegativeSign = "~";
         return numberFormat;
      }
      else
      {
         return null;
      }      
   }
}

A BigInteger object can then be instantiated with the following code:

BigInteger number = BigInteger.Parse("~6354129876", new BigIntegerFormatProvider());
// Display value using same formatting information
Console.WriteLine(number.ToString(new BigIntegerFormatProvider()));
// Display value using formatting of current culture
Console.WriteLine(number);

The second example is more straightforward. It passes the NumberFormatInfo object that provides formatting information to the provider parameter.

NumberFormatInfo fmt = new NumberFormatInfo();
fmt.NegativeSign = "~";

BigInteger number = BigInteger.Parse("~6354129876", fmt);
// Display value using same formatting information
Console.WriteLine(number.ToString(fmt));
// Display value using formatting of current culture
Console.WriteLine(number);

.NET Framework

Supported in: 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.