C# Int64 Parse(String, NumberStyles, IFormatProvider)
Description
Int64 Parse(String, NumberStyles, IFormatProvider)
converts
the string representation of a number in a specified style and culture-specific
format to its 64-bit signed integer equivalent.
Syntax
Int64.Parse(String, NumberStyles, IFormatProvider)
has the following syntax.
public static long Parse(
string s,/*from w ww . j a va 2s . c o m*/
NumberStyles style,
IFormatProvider provider
)
Parameters
Int64.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 Integer.provider
- An IFormatProvider that supplies culture-specific formatting information about s.
Returns
Int64.Parse(String, NumberStyles, IFormatProvider)
method returns A 64-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 Int64 values.
/* w ww. j a v a2 s . c om*/
using System;
using System.Globalization;
public class ParseInt64
{
public static void Main()
{
Convert("12,345", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("en-GB"));
Convert("12,345", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("fr-FR"));
Convert("12,345", NumberStyles.Float, new CultureInfo("en-US"));
Convert("12 345,00", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("sv-SE"));
Convert("12,345.00", NumberStyles.Float | NumberStyles.AllowThousands,
NumberFormatInfo.InvariantInfo);
Convert("123,456", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
new CultureInfo("fr-FR"));
Convert("123,456", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
new CultureInfo("en-US"));
Convert("123,456", NumberStyles.Integer | NumberStyles.AllowThousands,
new CultureInfo("en-US"));
}
private static void Convert(string value, NumberStyles style,
IFormatProvider provider)
{
try
{
long number = Int64.Parse(value, style, provider);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}'.", value);
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is out of range of the Int64 type.", value);
}
}
}
The code above generates the following result.