C# Int64 TryParse(String, NumberStyles, IFormatProvider, Int64)
Description
Int64 TryParse(String, NumberStyles, IFormatProvider, Int64)
converts the string representation of a number in a specified style
and culture-specific format to its 64-bit signed integer equivalent. A
return value indicates whether the conversion succeeded or failed.
Syntax
Int64.TryParse(String, NumberStyles, IFormatProvider, Int64)
has the following syntax.
public static bool TryParse(
string s,/*from ww w . j av a 2 s . com*/
NumberStyles style,
IFormatProvider provider,
out long result
)
Parameters
Int64.TryParse(String, NumberStyles, IFormatProvider, Int64)
has the following parameters.
s
- A string containing 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 formatting information about s.result
- When this method returns, contains the 64-bit signed integer value equivalent of the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null or String.Empty, is not in a format compliant with style, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.
Returns
Int64.TryParse(String, NumberStyles, IFormatProvider, Int64)
method returns true if s was converted successfully; otherwise, false.
Example
The following example calls the TryParse(String, NumberStyles, IFormatProvider, Int64) method with a number of different string and NumberStyles values.
using System;/*from w w w . j a va 2 s .c o m*/
using System.Globalization;
public class StringParsing
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "123456";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
numericString = "-12345";
styles = NumberStyles.None;
CallTryParse(numericString, styles);
styles = NumberStyles.AllowLeadingSign;
CallTryParse(numericString, styles);
numericString = "123456-";
CallTryParse(numericString, styles);
styles = styles | NumberStyles.AllowTrailingSign;
CallTryParse(numericString, styles);
numericString = "$12345";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
styles = NumberStyles.Integer | NumberStyles.AllowCurrencySymbol;
CallTryParse(numericString, styles);
numericString = "12345.00";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "12345.72";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "12,345";
styles = NumberStyles.Integer | NumberStyles.AllowThousands;
CallTryParse(numericString, styles);
numericString = "12E-01";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(numericString, styles);
numericString = "12E03";
CallTryParse(numericString, styles);
numericString = "80c1";
CallTryParse(numericString, NumberStyles.HexNumber);
numericString = "0x80C1";
CallTryParse(numericString, NumberStyles.HexNumber);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
int number;
CultureInfo provider;
// If currency symbol is allowed, use en-US culture.
if ((styles & NumberStyles.AllowCurrencySymbol) > 0)
provider = new CultureInfo("en-US");
else
provider = CultureInfo.InvariantCulture;
bool result = Int32.TryParse(stringToConvert, styles,
provider, out number);
if (result)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
else
Console.WriteLine("Attempted conversion of '{0}' failed.",
Convert.ToString(stringToConvert));
}
}
The code above generates the following result.