C# Int16 Parse(String, IFormatProvider)
Description
Int16 Parse(String, IFormatProvider)
converts the
string representation of a number in a specified culture-specific format
to its 16-bit signed integer equivalent.
Syntax
Int16.Parse(String, IFormatProvider)
has the following syntax.
public static short Parse(
string s,
IFormatProvider provider
)
Parameters
Int16.Parse(String, IFormatProvider)
has the following parameters.
s
- A string containing a number to convert.provider
- An IFormatProvider that supplies culture-specific formatting information about s.
Returns
Int16.Parse(String, IFormatProvider)
method returns A 16-bit signed integer equivalent to the number specified in s.
Example
The following example parses string representations of Int16 values with the Int16.Parse(String, IFormatProvider) method.
using System.Globalization;
using System;//from www . ja v a 2s . c o m
public class MainClass{
public static void Main(String[] argv){
string stringToConvert;
short number;
stringToConvert = " 123 ";
try
{
number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture);
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException)
{
Console.WriteLine("'{0'} is out of range of the Int16 data type.",
stringToConvert);
}
stringToConvert = " + 123";
try
{
number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture);
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException)
{
Console.WriteLine("'{0'} is out of range of the Int16 data type.",
stringToConvert);
}
stringToConvert = " +123 ";
try
{
number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture);
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException)
{
Console.WriteLine("'{0'} is out of range of the Int16 data type.",
stringToConvert);
}
}
}
The code above generates the following result.