Export (0) Print
Expand All

BigInteger.Parse Method (String, NumberStyles)

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)

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

Parameters

value
Type: System.String

A string that contains a number to convert.

style
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.

ExceptionCondition
ArgumentException

style is not a NumberStyles value.

-or-

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

ArgumentNullException

value is Nothing.

FormatException

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:

[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]

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

[ws]hexdigits[ws]

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

Element

Description

ws

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.

sign

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.

digits

fractional_digits

exponential_digits

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.

E

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.

hexdigits

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

None

The digits element only.

AllowDecimalPoint

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

AllowExponent

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

AllowLeadingWhite

The ws element at the start of value.

AllowTrailingWhite

The ws element at the end of value.

AllowLeadingSign

The sign element at the start of value.

AllowTrailingSign

The sign element at the end of value.

AllowParentheses

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

AllowThousands

The group separator (,) element.

AllowCurrencySymbol

The currency ($) element.

Currency

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

Float

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.

Number

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

Any

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

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.

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.)

NoteNote

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.

Imports System.Globalization
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim hexStrings() As String = { "80", "E293", "F9A2FF", "FFFFFFFF", 
                                     "080", "0E293", "0F9A2FF", "0FFFFFFFF",  
                                     "0080", "00E293", "00F9A2FF", "00FFFFFFFF" }
      For Each hexString As String In hexStrings
         Dim number As BigInteger = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier)
         Console.WriteLine("Converted 0x{0} to {1}.", hexString, number)
      Next          
   End Sub 
End Module 
' 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.

Dim number As BigInteger 
' Method should succeed (white space and sign allowed)
number = BigInteger.Parse("   -68054   ", NumberStyles.Integer)
Console.WriteLine(number)
' Method should succeed (string interpreted as hexadecimal)
number = BigInteger.Parse("68054", NumberStyles.AllowHexSpecifier)
Console.WriteLine(number)
' Method call should fail: sign not allowed 
Try
   number = BigInteger.Parse("   -68054  ", NumberStyles.AllowLeadingWhite _
                                            Or NumberStyles.AllowTrailingWhite)
   Console.WriteLine(number)
Catch e As FormatException
   Console.WriteLine(e.Message)
End Try                                                      
' Method call should fail: white space not allowed 
Try
   number = BigInteger.Parse("   68054  ", NumberStyles.AllowLeadingSign)
   Console.WriteLine(number)
Catch e As FormatException
   Console.WriteLine(e.Message)
End Try     

' The method produces the following output: 

'     -68054 
'     426068 
'     Input string was not in a correct format. 
'     Input string was not in a correct format.                                                 

.NET Framework

Supported in: 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 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.

Show:
© 2014 Microsoft