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