SByte.Parse Method

Definition

Converts the string representation of a number to its 8-bit signed integer equivalent.

Overloads

Parse(String, NumberStyles, IFormatProvider)

Converts the string representation of a number that is in a specified style and culture-specific format to its 8-bit signed equivalent.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Converts the span representation of a number that is in a specified style and culture-specific format to its 8-bit signed equivalent.

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Parses a span of UTF-8 characters into a value.

Parse(String, IFormatProvider)

Converts the string representation of a number in a specified culture-specific format to its 8-bit signed integer equivalent.

Parse(String)

Converts the string representation of a number to its 8-bit signed integer equivalent.

Parse(ReadOnlySpan<Char>, IFormatProvider)

Parses a span of characters into a value.

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Parses a span of UTF-8 characters into a value.

Parse(String, NumberStyles)

Converts the string representation of a number in a specified style to its 8-bit signed integer equivalent.

Parse(String, NumberStyles, IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

Important

This API is not CLS-compliant.

CLS-compliant alternative
System.Int16.Parse(String, NumberStyles, IFormatProvider)

Converts the string representation of a number that is in a specified style and culture-specific format to its 8-bit signed equivalent.

[System.CLSCompliant(false)]
public static sbyte Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static sbyte Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static sbyte Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);

Parameters

s
String

A string that contains the number to convert. The string is interpreted by using the style specified by style.

style
NumberStyles

A bitwise combination of the enumeration values that indicates the style elements that can be present in s. A typical value to specify is Integer.

provider
IFormatProvider

An object that supplies culture-specific formatting information about s. If provider is null, the thread current culture is used.

Returns

An 8-bit signed byte value that is equivalent to the number specified in the s parameter.

Implements

Attributes

Exceptions

style is not a NumberStyles value.

-or-

style is not a combination of AllowHexSpecifier and HexNumber.

s is not in a format that is compliant with style.

s represents a number that is less than SByte.MinValue or greater than SByte.MaxValue.

-or-

s includes non-zero, fractional digits.

Examples

The following example illustrates the use of the Parse(String, NumberStyles, IFormatProvider) method to convert various string representations of numbers to signed integer values.

using System;
using System.Globalization;

public class SByteConversion
{
   NumberFormatInfo provider = NumberFormatInfo.CurrentInfo;

   public static void Main()
   {
      string stringValue;
      NumberStyles style;

      stringValue = "   123   ";
      style = NumberStyles.None;     
      CallParseOperation(stringValue, style);
      
      stringValue = "000,000,123";
      style = NumberStyles.Integer | NumberStyles.AllowThousands;
      CallParseOperation(stringValue, style);
      
      stringValue = "-100";
      style = NumberStyles.AllowLeadingSign;
      CallParseOperation(stringValue, style);
      
      stringValue = "100-";
      style = NumberStyles.AllowLeadingSign;
      CallParseOperation(stringValue, style);
      
      stringValue = "100-";
      style = NumberStyles.AllowTrailingSign;
      CallParseOperation(stringValue, style);
      
      stringValue = "$100";
      style = NumberStyles.AllowCurrencySymbol;
      CallParseOperation(stringValue, style);
      
      style = NumberStyles.Integer;
      CallParseOperation(stringValue, style);
      
      style = NumberStyles.AllowDecimalPoint;
      CallParseOperation("100.0", style);
      
      stringValue = "1e02";
      style = NumberStyles.AllowExponent;
      CallParseOperation(stringValue, style);
      
      stringValue = "(100)";
      style = NumberStyles.AllowParentheses;
      CallParseOperation(stringValue, style);
   }
   
   private static void CallParseOperation(string stringValue, 
                                          NumberStyles style)
   {                                          
      sbyte number;
      
      if (stringValue == null)
         Console.WriteLine("Cannot parse a null string...");
         
      try
      {
         number = sbyte.Parse(stringValue, style);
         Console.WriteLine("SByte.Parse('{0}', {1})) = {2}", 
                           stringValue, style, number);   
      }
      catch (FormatException)
      {
         Console.WriteLine("'{0}' and {1} throw a FormatException", 
                           stringValue, style);   
      }      
      catch (OverflowException)
      {
         Console.WriteLine("'{0}' is outside the range of a signed byte",
                           stringValue);
      }
   }
}
// The example displays the following information to the console:
//       '   123   ' and None throw a FormatException
//       SByte.Parse('000,000,123', Integer, AllowThousands)) = 123
//       SByte.Parse('-100', AllowLeadingSign)) = -100
//       '100-' and AllowLeadingSign throw a FormatException
//       SByte.Parse('100-', AllowTrailingSign)) = -100
//       SByte.Parse('$100', AllowCurrencySymbol)) = 100
//       '$100' and Integer throw a FormatException
//       SByte.Parse('100.0', AllowDecimalPoint)) = 100
//       SByte.Parse('1e02', AllowExponent)) = 100
//       SByte.Parse('(100)', AllowParentheses)) = -100

Remarks

The style parameter defines the style elements (such as white space or the positive or negative sign symbol) that are allowed in the s parameter for the parse operation to succeed. It must be a combination of bit flags from the NumberStyles enumeration.

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

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

If style includes AllowHexSpecifier, the s parameter may include 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 beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ A culture-specific currency symbol. Its position in the string is defined by the NumberFormatInfo.CurrencyPositivePattern property of the current culture. The current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
sign An optional sign. The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag, and it can appear the end of s if style includes the NumberStyles.AllowTrailingSign flag. Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
digits A sequence of digits from 0 through 9.
. A culture-specific decimal point symbol. The current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.
fractional_digits One or more occurrences of the digit 0-9 if style includes the NumberStyles.AllowExponent flag, or one or more occurrences of the digit 0 if it does not. Fractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag.
E The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
exponential_digits A sequence of digits from 0 through 9. The s 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.

Note

Any terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

A string with decimal 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 this input string. The following table indicates how individual NumberStyles members affect the elements that may be present in s.

Non-composite NumberStyles values Elements permitted in s in addition to digits
NumberStyles.None Decimal digits only.
NumberStyles.AllowDecimalPoint The decimal point (.) and fractional_digits elements. However, if style does not include the NumberStyles.AllowExponent flag, fractional_digits must consist of only one or more 0 digits; otherwise, an OverflowException is thrown.
NumberStyles.AllowExponent The "e" or "E" character, which indicates exponential notation, along with exponential_digits.
NumberStyles.AllowLeadingWhite The ws element at the beginning of s.
NumberStyles.AllowTrailingWhite The ws element at the end of s.
NumberStyles.AllowLeadingSign A positive sign before digits.
NumberStyles.AllowTrailingSign A positive sign after digits.
NumberStyles.AllowParentheses Parentheses before and after digits to indicate a negative value.
NumberStyles.AllowThousands The group separator (,) element. Although the group separator can appear in s, it must be preceded by only one or more 0 digits.
NumberStyles.AllowCurrencySymbol The currency ($) element.

If the NumberStyles.AllowHexSpecifier flag is used, s must be a hexadecimal value. Valid hexadecimal digits are 0-9, a-f, and A-F. The only other flags that can be combined with it are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. (The NumberStyles enumeration includes a composite number style, NumberStyles.HexNumber, that includes both white-space flags.)

Note

If the s parameter 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 parse operation to throw an exception.

If s represents a hexadecimal number, the Parse(String, NumberStyles) method interprets the high-order bit of the byte as a sign bit.

The provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of s. There are three ways to use the provider parameter to supply custom formatting information to the parse operation:

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

  • You can pass a CultureInfo object that specifies the culture whose formatting is to be used. Its NumberFormat property provides formatting information.

  • You can pass a custom IFormatProvider implementation. Its GetFormat method must instantiate and return the NumberFormatInfo object that provides formatting information.

If provider is null, the NumberFormatInfo object for the current culture is used.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

Important

This API is not CLS-compliant.

Converts the span representation of a number that is in a specified style and culture-specific format to its 8-bit signed equivalent.

public static sbyte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
[System.CLSCompliant(false)]
public static sbyte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
[System.CLSCompliant(false)]
public static sbyte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);

Parameters

s
ReadOnlySpan<Char>

A span containing the characters representing the number to convert. The span is interpreted by using the style specified by style.

style
NumberStyles

A bitwise combination of the enumeration values that indicates the style elements that can be present in s. A typical value to specify is Integer.

provider
IFormatProvider

An object that supplies culture-specific formatting information about s. If provider is null, the thread current culture is used.

Returns

An 8-bit signed byte value that is equivalent to the number specified in the s parameter.

Implements

Attributes

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs

Parses a span of UTF-8 characters into a value.

public static sbyte Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);

Parameters

utf8Text
ReadOnlySpan<Byte>

The span of UTF-8 characters to parse.

style
NumberStyles

A bitwise combination of number styles that can be present in utf8Text.

provider
IFormatProvider

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

Returns

The result of parsing utf8Text.

Implements

Applies to

.NET 9 and .NET 8
Product Versions
.NET 8, 9

Parse(String, IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

Important

This API is not CLS-compliant.

CLS-compliant alternative
System.Int16.Parse(String)

Converts the string representation of a number in a specified culture-specific format to its 8-bit signed integer equivalent.

[System.CLSCompliant(false)]
public static sbyte Parse (string s, IFormatProvider provider);
public static sbyte Parse (string s, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static sbyte Parse (string s, IFormatProvider? provider);

Parameters

s
String

A string that represents a number to convert. The string is interpreted using the Integer style.

provider
IFormatProvider

An object that supplies culture-specific formatting information about s. If provider is null, the thread current culture is used.

Returns

An 8-bit signed integer that is equivalent to the number specified in s.

Implements

Attributes

Exceptions

s is not in the correct format.

s represents a number less than SByte.MinValue or greater than SByte.MaxValue.

Examples

The following example defines a custom NumberFormatInfo object that defines the tilde (~) as the negative sign. It then parses a number of numeric strings using this custom NumberFormatInfo object as well as a CultureInfo object that represents the invariant culture.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      NumberFormatInfo nf = new NumberFormatInfo();
      nf.NegativeSign = "~"; 
      
      string[] values = { "-103", "+12", "~16", "  1", "~255" };
      IFormatProvider[] providers = { nf, CultureInfo.InvariantCulture };
      
      foreach (IFormatProvider provider in providers)
      {
         Console.WriteLine("Conversions using {0}:", ((object) provider).GetType().Name);
         foreach (string value in values)
         {
            try {
               Console.WriteLine("   Converted '{0}' to {1}.", 
                                 value, SByte.Parse(value, provider));
            }                     
            catch (FormatException) {
               Console.WriteLine("   Unable to parse '{0}'.", value);   
            }
            catch (OverflowException) {
               Console.WriteLine("   '{0}' is out of range of the SByte type.", value);         
            }
         }
      }      
   }
}
// The example displays the following output:
//       Conversions using NumberFormatInfo:
//          Unable to parse '-103'.
//          Converted '+12' to 12.
//          Converted '~16' to -16.
//          Converted '  1' to 1.
//          '~255' is out of range of the SByte type.
//       Conversions using CultureInfo:
//          Converted '-103' to -103.
//          Converted '+12' to 12.
//          Unable to parse '~16'.
//          Converted '  1' to 1.
//          Unable to parse '~255'.

Remarks

The s parameter contains a number of the 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.
digits A sequence of digits ranging from 0 to 9.

The s parameter is interpreted using the Integer style. In addition to the byte value's 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 s, use the Parse(String, NumberStyles, IFormatProvider) method.

The provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of s. There are three ways to use the provider parameter to supply custom formatting information to the parse operation:

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

  • You can pass a CultureInfo object that specifies the culture whose formatting is to be used. Its NumberFormat property provides formatting information.

  • You can pass a custom IFormatProvider implementation. Its GetFormat method must instantiate and return the NumberFormatInfo object that provides formatting information.

If provider is null, the NumberFormatInfo object for the current culture is used.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(String)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

Important

This API is not CLS-compliant.

CLS-compliant alternative
System.Int16.Parse(String)

Converts the string representation of a number to its 8-bit signed integer equivalent.

[System.CLSCompliant(false)]
public static sbyte Parse (string s);
public static sbyte Parse (string s);

Parameters

s
String

A string that represents a number to convert. The string is interpreted using the Integer style.

Returns

An 8-bit signed integer that is equivalent to the number contained in the s parameter.

Attributes

Exceptions

s is null.

s does not consist of an optional sign followed by a sequence of digits (zero through nine).

s represents a number less than SByte.MinValue or greater than SByte.MaxValue.

Examples

The following example demonstrates how to convert a string value into a signed byte value using the Parse method. The resulting signed byte value is then displayed to the console.

// Define an array of numeric strings.
string[] values = { "-16", "  -3", "+ 12", " +12 ", "  12  ",
                    "+120", "(103)", "192", "-160" };
                           
// Parse each string and display the result.
foreach (string value in values)
{
   try {
      Console.WriteLine("Converted '{0}' to the SByte value {1}.",
                        value, SByte.Parse(value));
   }
   catch (FormatException) {
      Console.WriteLine("'{0}' cannot be parsed successfully by SByte type.",
                        value);
   }                              
   catch (OverflowException) {
      Console.WriteLine("'{0}' is out of range of the SByte type.",
                        value);
   }                                                                        
}
// The example displays the following output:
//       Converted '-16' to the SByte value -16.
//       Converted '  -3' to the SByte value -3.
//       '+ 12' cannot be parsed successfully by SByte type.
//       Converted ' +12 ' to the SByte value 12.
//       Converted '  12  ' to the SByte value 12.
//       Converted '+120' to the SByte value 120.
//       '(103)' cannot be parsed successfully by SByte type.
//       '192' is out of range of the SByte type.
//       '-160' is out of range of the SByte type.

Remarks

The s parameter contains a number of the 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.
digits A sequence of digits ranging from 0 to 9.

The s parameter is interpreted using the NumberStyles.Integer style. In addition to the byte value's decimal digits, only leading and trailing spaces with a leading positive or negative sign are allowed. To explicitly define the style elements that can be present in s, use either the Parse(String, NumberStyles) or the Parse(String, NumberStyles, IFormatProvider) method.

The s parameter is parsed by using the formatting information in a NumberFormatInfo that is initialized for the current system culture. For more information, see NumberFormatInfo.CurrentInfo. To parse a string by using the formatting information of some other culture, use the Parse(String, NumberStyles, IFormatProvider) method.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(ReadOnlySpan<Char>, IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

Parses a span of characters into a value.

public static sbyte Parse (ReadOnlySpan<char> s, IFormatProvider? provider);

Parameters

s
ReadOnlySpan<Char>

The span of characters to parse.

provider
IFormatProvider

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

Returns

The result of parsing s.

Implements

Applies to

.NET 9 and other versions
Product Versions
.NET 7, 8, 9

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs

Parses a span of UTF-8 characters into a value.

public static sbyte Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);

Parameters

utf8Text
ReadOnlySpan<Byte>

The span of UTF-8 characters to parse.

provider
IFormatProvider

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

Returns

The result of parsing utf8Text.

Implements

Applies to

.NET 9 and .NET 8
Product Versions
.NET 8, 9

Parse(String, NumberStyles)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

Important

This API is not CLS-compliant.

CLS-compliant alternative
System.Int16.Parse(String)

Converts the string representation of a number in a specified style to its 8-bit signed integer equivalent.

[System.CLSCompliant(false)]
public static sbyte Parse (string s, System.Globalization.NumberStyles style);
public static sbyte Parse (string s, System.Globalization.NumberStyles style);

Parameters

s
String

A string that contains a number to convert. The string is interpreted using the style specified by style.

style
NumberStyles

A bitwise combination of the enumeration values that indicates the style elements that can be present in s. A typical value to specify is Integer.

Returns

An 8-bit signed integer that is equivalent to the number specified in s.

Attributes

Exceptions

s is not in a format that is compliant with style.

s represents a number less than SByte.MinValue or greater than SByte.MaxValue.

-or-

s includes non-zero, fractional digits.

style is not a NumberStyles value.

-or-

style is not a combination of AllowHexSpecifier and HexNumber values.

Examples

The following example parses string representations of SByte values with the Parse(String, NumberStyles) method. The current culture for the example is en-US.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      NumberStyles style;
      sbyte number;

      // Parse value with no styles allowed.
      string[] values1 = { " 121 ", "121", "-121" };
      style = NumberStyles.None;
      Console.WriteLine("Styles: {0}", style.ToString());
      foreach (string value in values1)
      {
         try {
            number = SByte.Parse(value, style);
            Console.WriteLine("   Converted '{0}' to {1}.", value, number);
         }   
         catch (FormatException) {
            Console.WriteLine("   Unable to parse '{0}'.", value);
         }
      }
      Console.WriteLine();
            
      // Parse value with trailing sign.
      style = NumberStyles.Integer | NumberStyles.AllowTrailingSign;
      string[] values2 = { " 103+", " 103 +", "+103", "(103)", "   +103  " };
      Console.WriteLine("Styles: {0}", style.ToString());
      foreach (string value in values2)
      {
         try {
            number = SByte.Parse(value, style);
            Console.WriteLine("   Converted '{0}' to {1}.", value, number);
         }   
         catch (FormatException) {
            Console.WriteLine("   Unable to parse '{0}'.", value);
         }      
         catch (OverflowException) {
            Console.WriteLine("   '{0}' is out of range of the SByte type.", value);         
         }
      }      
      Console.WriteLine();
   }
}
// The example displays the following output:
//       Styles: None
//          Unable to parse ' 121 '.
//          Converted '121' to 121.
//          Unable to parse '-121'.
//       
//       Styles: Integer, AllowTrailingSign
//          Converted ' 103+' to 103.
//          Converted ' 103 +' to 103.
//          Converted '+103' to 103.
//          Unable to parse '(103)'.
//          Converted '   +103  ' to 103.

Remarks

The style parameter defines the style elements (such as white space or the positive or negative sign symbol) that are allowed in the s parameter for the parse operation to succeed. It must be a combination of bit flags from the NumberStyles enumeration.

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

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

If style includes NumberStyles.AllowHexSpecifier, the s 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 beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ A culture-specific currency symbol. Its position in the string is defined by the NumberFormatInfo.CurrencyPositivePattern property of the current culture. The current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
sign An optional sign. The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingSign flag. Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
digits A sequence of digits from 0 through 9.
. A culture-specific decimal point symbol. The current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.
fractional_digits One or more occurrences of the digit 0-9 if style includes the NumberStyles.AllowExponent flag, or one or more occurrences of the digit 0 if it does not. Fractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag.
E The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
exponential_digits One or more occurrences of the digit 0-9. The s 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.

Note

Any terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

A string with decimal 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 s.

Non-composite NumberStyles values Elements permitted in s in addition to digits
NumberStyles.None Decimal digits only.
NumberStyles.AllowDecimalPoint The decimal point (.) and fractional_digits elements. However, if style does not include the NumberStyles.AllowExponent flag, fractional_digits must consist of only one or more 0 digits; otherwise, an OverflowException is thrown.
NumberStyles.AllowExponent The "e" or "E" character, which indicates exponential notation, along with exponential_digits.
NumberStyles.AllowLeadingWhite The ws element at the beginning of s.
NumberStyles.AllowTrailingWhite The ws element at the end of s.
NumberStyles.AllowLeadingSign A positive sign before digits.
NumberStyles.AllowTrailingSign A positive sign after digits.
NumberStyles.AllowParentheses The sign element in the form of parentheses enclosing the numeric value.
NumberStyles.AllowThousands The group separator (,) element. Although the group separator can appear in s, it must be preceded by only one or more 0 digits.
NumberStyles.AllowCurrencySymbol The currency ($) element.

If the NumberStyles.AllowHexSpecifier flag is used, s must be a hexadecimal value. Valid hexadecimal digits are 0-9, a-f, and A-F. A prefix such as "0x" is not supported and causes the parse operation to fail. The only other flags that can be combined included in style are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. (The NumberStyles enumeration includes a composite number style, NumberStyles.HexNumber, that includes both white-space flags.)

Note

If the s parameter 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 parse operation to throw an exception.

If s represents a hexadecimal number, the Parse(String, NumberStyles) method interprets the high-order bit of the byte as a sign bit.

The s parameter is parsed by using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. To use the formatting information of some other culture, call the Parse(String, NumberStyles, IFormatProvider) overload.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0