C# Byte Parse(String, NumberStyles, IFormatProvider)
Description
Byte Parse(String, NumberStyles, IFormatProvider)
converts
the string representation of a number in a specified style and culture-specific
format to its Byte equivalent.
Syntax
Byte.Parse(String, NumberStyles, IFormatProvider)
has the following syntax.
public static byte Parse(
string s,//from ww w. j a v a 2 s .c o m
NumberStyles style,
IFormatProvider provider
)
Parameters
Byte.Parse(String, NumberStyles, IFormatProvider)
has the following parameters.
s
- A string that contains a number to convert. The string is interpreted using the style specified by style.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 object that supplies culture-specific information about the format of s. If provider is null, the thread current culture is used.
Returns
Byte.Parse(String, NumberStyles, IFormatProvider)
method returns A byte value that is equivalent to the number contained in s.
Example
The following code example parses string representations of Byte values with this overload of the Byte.Parse(String, NumberStyles, IFormatProvider) method.
using System;//from ww w . java 2 s.c om
using System.Globalization;
public class MainClass {
public static void Main(String[] argv){
NumberStyles style;
CultureInfo culture;
string value;
byte number;
// Parse number with decimals.
// NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
style = NumberStyles.Float;
culture = CultureInfo.CreateSpecificCulture("fr-FR");
value = "12,000";
number = Byte.Parse(value, style, culture);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
culture = CultureInfo.CreateSpecificCulture("en-GB");
try
{
number = Byte.Parse(value, style, culture);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
Console.WriteLine("Unable to parse '{0}'.", value); }
value = "12.000";
number = Byte.Parse(value, style, culture);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
}
The code above generates the following result.