C# Int16 Parse(String, NumberStyles, IFormatProvider)
Description
Int16 Parse(String, NumberStyles, IFormatProvider)
converts
the string representation of a number in a specified style and culture-specific
format to its 16-bit signed integer equivalent.
Syntax
Int16.Parse(String, NumberStyles, IFormatProvider)
has the following syntax.
public static short Parse(
string s,// w ww.ja v a2 s. co m
NumberStyles style,
IFormatProvider provider
)
Parameters
Int16.Parse(String, NumberStyles, IFormatProvider)
has the following parameters.
s
- A string containing a number to convert.style
- A bitwise combination of enumeration values that indicates the style elements that can be present in s. A typical value to specify is NumberStyles.Integer.provider
- An IFormatProvider that supplies culture-specific formatting information about s.
Returns
Int16.Parse(String, NumberStyles, IFormatProvider)
method returns A 16-bit signed integer equivalent to the number specified in s.
Example
The following example uses a variety of style and provider parameters to parse the string representations of Int16 values.
/*from www . ja va 2 s. c om*/
using System;
using System.Globalization;
public class MainClass{
public static void Main(String[] argv){
string value;
short number;
NumberStyles style;
CultureInfo provider;
// Parse string using "." as the thousands separator
// and " " as the decimal separator.
value = "12 345,00";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
provider = new CultureInfo("fr-FR");
number = Int16.Parse(value, style, provider);
Console.WriteLine("'{0}' converted to {1}.", value, number);
try
{
number = Int16.Parse(value, style, CultureInfo.InvariantCulture);
Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", value);
}
// Parse string using "$" as the currency symbol for en_GB and
// en-US cultures.
value = "$1,234.00";
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
provider = new CultureInfo("en-GB");
try
{
number = Int16.Parse(value, style, CultureInfo.InvariantCulture);
Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", value);
}
provider = new CultureInfo("en-US");
number = Int16.Parse(value, style, provider);
Console.WriteLine("'{0}' converted to {1}.", value, number);
}
}
The code above generates the following result.