BigInteger.Parse Method (String, NumberStyles)
Converts the string representation of a number in a specified style to its BigInteger equivalent.
Assembly: System.Numerics (in System.Numerics.dll)
- 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 ValueType: System.Numerics.BigInteger
A value that is equivalent to the number specified in the value parameter.
style is not a NumberStyles value.
value is a null reference (Nothing in Visual Basic).
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.
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 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.
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 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.
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.