BigInteger.Parse Method (String, NumberStyles)

.NET Framework (current version)

The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

public static BigInteger Parse(
	string value,
	NumberStyles style


Type: System.String

A string that contains a number to convert.

Type: System.Globalization.NumberStyles

A bitwise combination of the enumeration values that specify the permitted format of value.

Return Value

Type: System.Numerics.BigInteger

A value that is equivalent to the number specified in the value parameter.

Exception Condition

style is not a NumberStyles value.


style includes the AllowHexSpecifier or HexNumber flag along with another value.


value is null.


value does not comply with the input pattern specified by NumberStyles.

The style parameter defines the style elements (such as white space, the positive or negative sign symbol, the group separator symbol, or the decimal point symbol) that are allowed in the value parameter for the parse operation to succeed. styles must be a combination of bit flags from the NumberStyles enumeration. The style parameter makes this method overload useful when value contains the string representation of a hexadecimal value, when the number system (decimal or hexadecimal) represented by value is known only at run time, or when you want to disallow white space or a sign symbol in value.

Depending on the value of style, the value parameter may include the following elements:


If style includes NumberStyles.AllowHexSpecifier, the value parameter may contain the following elements:


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




Optional white space. White space can appear at the start of value if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of value if style includes the NumberStyles.AllowTrailingWhite flag.


A culture-specific currency symbol. Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern properties of the current culture. The current culture's currency symbol can appear in value if style includes the NumberStyles.AllowCurrencySymbol flag.


An optional sign. The sign can appear at the start of value if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of value if style includes the NumberStyles.AllowTrailingSign flag. Parentheses can be used in value to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.




A sequence of digits from 0 through 9. For fractional_digits, only the digit 0 is valid.


A culture-specific group separator symbol. The current culture's group separator can appear in value if style includes the NumberStyles.AllowThousands flag.


A culture-specific decimal point symbol. The current culture's decimal point symbol can appear in value if style includes the NumberStyles.AllowDecimalPoint flag. Only the digit 0 can appear as a fractional digit for the parse operation to succeed; if fractional_digits includes any other digit, a FormatException is thrown.


The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. The value parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.


A sequence of hexadecimal digits from 0 through f, or 0 through F.

A string with digits only (which corresponds to the NumberStyles.None style) always parses successfully. Most of the remaining NumberStyles members control elements that may be present, but are not required to be present, in the input string. The following table indicates how individual NumberStyles members affect the elements that may be present in value.

NumberStyles value

Elements permitted in value in addition to digits


The digits element only.


The decimal point (.) and fractional-digits elements.


The "e" or "E" character, which indicates exponential notation, along with exponential_digits.


The ws element at the start of value.


The ws element at the end of value.


The sign element at the start of value.


The sign element at the end of value.


The sign element in the form of parentheses enclosing the numeric value.


The group separator (,) element.


The currency ($) element.


All elements. However, value cannot represent a hexadecimal number or a number in exponential notation.


The ws element at the start or end of value, sign at the start of value, and the decimal point (.) symbol. The value parameter can also use exponential notation.


The ws, sign, group separator (,), and decimal point (.) elements.


All elements. However, value cannot represent a hexadecimal number.


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.

Unlike the other NumberStyles values, which allow for, but do not require, the presence of particular style elements in value, the NumberStyles.AllowHexSpecifier style value means that the individual numeric characters in value are always interpreted as hexadecimal characters. Valid hexadecimal characters are 0-9, A-F, and a-f. The only other flags that can be combined with the style parameter are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. (The NumberStyles enumeration includes a composite number style, HexNumber, that includes both white-space flags.)


If value is the string representation of a hexadecimal number, it cannot be preceded by any decoration (such as 0x or &h) that differentiates it as a hexadecimal number. This causes the conversion to fail.

If value is a hexadecimal string, the Parse(String, NumberStyles) method interprets value as a negative number stored by using two's complement representation if its first two hexadecimal digits are greater than or equal to 0x80. In other words, the method interprets the highest-order bit of the first byte in value as the sign bit. To make sure that a hexadecimal string is correctly interpreted as a positive number, the first digit in value must have a value of zero. For example, the method interprets 0x80 as a negative value, but it interprets either 0x080 or 0x0080 as a positive value. The following example illustrates the difference between hexadecimal strings that represent negative and positive values.

using System;
using System.Globalization;
using System.Numerics;

public class Example
   public static void Main()
      string[] hexStrings = { "80", "E293", "F9A2FF", "FFFFFFFF", 
                              "080", "0E293", "0F9A2FF", "0FFFFFFFF",  
                              "0080", "00E293", "00F9A2FF", "00FFFFFFFF" };
      foreach (string hexString in hexStrings)
         BigInteger number = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier);
         Console.WriteLine("Converted 0x{0} to {1}.", hexString, number);
// The example displays the following output:
//       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.

The value parameter is parsed by using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. To specify the culture whose formatting information is used for the parse operation, call the Parse(String, NumberStyles, IFormatProvider) overload.

The following example illustrates calls to the Parse(String, NumberStyles) method with several possible values for the style parameter. It illustrates how to interpret a string as a hexadecimal value, and how to disallow spaces and sign symbols.

BigInteger number; 
// Method should succeed (white space and sign allowed)
number = BigInteger.Parse("   -68054   ", NumberStyles.Integer);
// Method should succeed (string interpreted as hexadecimal)
number = BigInteger.Parse("68054", NumberStyles.AllowHexSpecifier);
// Method call should fail: sign not allowed
   number = BigInteger.Parse("   -68054  ", NumberStyles.AllowLeadingWhite 
                                            | NumberStyles.AllowTrailingWhite);
catch (FormatException e)
// Method call should fail: white space not allowed
   number = BigInteger.Parse("   68054  ", NumberStyles.AllowLeadingSign);
catch (FormatException e)
// The method produces the following output:
//     -68054
//     426068
//     Input string was not in a correct format.
//     Input string was not in a correct format.                                                 

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
Return to top