SByte.Parse(String, NumberStyles, IFormatProvider) converts string to its 8-bit signed integer. : sbyte « Data Types « C# / C Sharp






SByte.Parse(String, NumberStyles, IFormatProvider) converts string to its 8-bit signed integer.

 

using System;
using System.Globalization;

public class SByteConversion
{
   NumberFormatInfo provider = NumberFormatInfo.CurrentInfo;

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

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

      stringValue = "000,000,1";
      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;
      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);   
      }      
   }
}

   
  








Related examples in the same category

1.SByte.MaxValue Represents the largest value of SByte.
2.SByte.MinValue Represents the smallest value of SByte.
3.SByte.Parse converts string to its 8-bit signed integer
4.SByte.Parse(String,IFormatProvider) converts string to its 8-bit signed integer
5.SByte.Parse(String, NumberStyles) converts string to its 8-bit signed integer
6.Parse value with trailing sign
7.SByte.ToString(IFormatProvider) converts numeric value to string with culture-specific format
8.SByte.ToString(String) converts numeric value to string using the specified format.
9.SByte.ToString(String, IFormatProvider) converts numeric value to string using the specified format
10.SByte.ToString converts the numeric value to string
11.SByte.TryParse(String, NumberStyles, IFormatProvider, SByte) tries to convert string to SByte
12.SByte.TryParse(String, SByte) tries to convert string to its SByte