C# SByte Parse(String, NumberStyles, IFormatProvider)

Description

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

Syntax

SByte.Parse(String, NumberStyles, IFormatProvider) has the following syntax.


[CLSCompliantAttribute(false)]//from w w w  .  j  a v  a 2 s.  co m
public static sbyte Parse(
  string s,
  NumberStyles style,
  IFormatProvider provider
)

Parameters

SByte.Parse(String, NumberStyles, IFormatProvider) has the following parameters.

  • s - A string that contains the number to convert. The string is interpreted by using the style specified by style.
  • style - A bitwise combination of the enumeration values that indicates the style elements that can be present in s. A typical value to specify is NumberStyles.Integer.
  • provider - An object that supplies culture-specific formatting information about s. If provider is null, the thread current culture is used.

Returns

SByte.Parse(String, NumberStyles, IFormatProvider) method returns An 8-bit signed byte value that is equivalent to the number specified in the s parameter.

Example

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;// ww w.j  av  a  2  s. c  om
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 = "-123";
      style = NumberStyles.AllowLeadingSign;
      CallParseOperation(stringValue, style);

      stringValue = "123-";
      style = NumberStyles.AllowLeadingSign;
      CallParseOperation(stringValue, style);

      stringValue = "123-";
      style = NumberStyles.AllowTrailingSign;
      CallParseOperation(stringValue, style);

      stringValue = "$123";
      style = NumberStyles.AllowCurrencySymbol;
      CallParseOperation(stringValue, style);

      style = NumberStyles.Integer;
      CallParseOperation(stringValue, style);

      style = NumberStyles.AllowDecimalPoint;
      CallParseOperation("123.0", style);

      stringValue = "1e02";
      style = NumberStyles.AllowExponent;
      CallParseOperation(stringValue, style);

      stringValue = "(123)";
      style = NumberStyles.AllowParentheses;
      CallParseOperation(stringValue, style);
   }

   private static void CallParseOperation(string stringValue, 
                                          NumberStyles style)
   {                                          
      sbyte number;

      try
      {
         number = sbyte.Parse(stringValue, style);
         Console.WriteLine("SByte.Parse('{0}', {1})) = {2}", 
                           stringValue, style.ToString(), number);   
      }
      catch (Exception e)
      {
         Console.WriteLine("'{0}' and {1} throw a {2}", 
                           stringValue, style.ToString(), e.GetType().Name);   
      }      
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version