C# UInt16 Parse(String, NumberStyles, IFormatProvider)
Description
UInt16 Parse(String, NumberStyles, IFormatProvider)
converts
the string representation of a number in a specified style and culture-specific
format to its 16-bit unsigned integer equivalent.
Syntax
UInt16.Parse(String, NumberStyles, IFormatProvider)
has the following syntax.
[CLSCompliantAttribute(false)]// w w w .j ava 2s . c o m
public static ushort Parse(
string s,
NumberStyles style,
IFormatProvider provider
)
Parameters
UInt16.Parse(String, NumberStyles, IFormatProvider)
has the following parameters.
s
- A string that represents the number to convert. The string is interpreted by using the style specified by the style parameter.style
- A bitwise combination of enumeration values that indicate the style elements that can be present in s. A typical value to specify is NumberStyles.Integer.provider
- An object that supplies culture-specific formatting information about s.
Returns
UInt16.Parse(String, NumberStyles, IFormatProvider)
method returns A 16-bit unsigned integer equivalent to the number specified in s.
Example
The following example uses the Parse(String, NumberStyles, IFormatProvider) method to convert various string representations of numbers to 16-bit unsigned integer values.
using System;//www. ja va2 s . c om
using System.Globalization;
public class Example
{
public static void Main()
{
string[] cultureNames = { "en-US", "fr-FR" };
NumberStyles[] styles= { NumberStyles.Integer,
NumberStyles.Integer | NumberStyles.AllowDecimalPoint };
string[] values = { "1234", "+1234.0", "+1234,0", "-1234.00",
"-1234,00", "1234.1", "1234,1" };
foreach (string cultureName in cultureNames)
{
CultureInfo ci = new CultureInfo(cultureName);
Console.WriteLine(ci.DisplayName);
foreach (NumberStyles style in styles)
{
Console.WriteLine(" Style: {0}", style.ToString());
foreach (string value in values)
{
try {
Console.WriteLine(" Converted '{0}' to {1}.", value,
UInt16.Parse(value, style, ci));
}
catch (FormatException) {
Console.WriteLine(" Unable to parse '{0}'.", value);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is out of range of the UInt16 type.",
value);
}
}
}
}
}
}
The code above generates the following result.