SByte.TryParse(String, NumberStyles, IFormatProvider, SByte) tries to convert string to SByte
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "106";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
numericString = "-106";
styles = NumberStyles.None;
CallTryParse(numericString, styles);
numericString = "123.00";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "123.72";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "110E-01";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(numericString, styles);
numericString = "123E-01";
CallTryParse(numericString, styles);
numericString = "0x8C";
CallTryParse(numericString, NumberStyles.HexNumber);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
sbyte number;
bool result = SByte.TryParse(stringToConvert, styles, CultureInfo.InvariantCulture, out number);
if (result)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
else
Console.WriteLine("Attempted conversion of '{0}' failed.", Convert.ToString(stringToConvert));
}
}
Related examples in the same category