Convert string with Exponential to short in CSharp
Description
The following code shows how to convert string with Exponential to short.
Example
/*from w w w . ja v a 2s. c o m*/
using System;
using System.Globalization;
public class StringParsing
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "12E-01";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(numericString, styles);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
Console.WriteLine(stringToConvert);
short number;
bool result = Int16.TryParse(stringToConvert, styles,CultureInfo.InvariantCulture, out number);
if (result)
Console.WriteLine(stringToConvert, number);
else
Console.WriteLine("failed.");
}
}
The code above generates the following result.