本文整理匯總了C#中System.Int64.TryParse方法的典型用法代碼示例。如果您正苦於以下問題:C# Int64.TryParse方法的具體用法?C# Int64.TryParse怎麽用?C# Int64.TryParse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Int64
的用法示例。
在下文中一共展示了Int64.TryParse方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
public class StringParsing
{
public static void Main()
{
TryToParse(null);
TryToParse("160519");
TryToParse("9432.0");
TryToParse("16,667");
TryToParse(" -322 ");
TryToParse("+4302");
TryToParse("(100);");
TryToParse("01FA");
}
private static void TryToParse(string value)
{
bool success = Int64.TryParse(value, out long number);
if (success)
{
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
else
{
if (value == null) value = "";
Console.WriteLine("Attempted conversion of '{0}' failed.", value);
}
}
}
輸出:
Attempted conversion of '' failed. Converted '160519' to 160519. Attempted conversion of '9432.0' failed. Attempted conversion of '16,667' failed. Converted ' -322 ' to -322. Converted '+4302' to 4302. Attempted conversion of '(100);' failed. Attempted conversion of '01FA' failed.
示例2: Main
//引入命名空間
using System;
using System.Globalization;
public class StringParsing
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "106779";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
numericString = "-30677";
styles = NumberStyles.None;
CallTryParse(numericString, styles);
styles = NumberStyles.AllowLeadingSign;
CallTryParse(numericString, styles);
numericString = "301677-";
CallTryParse(numericString, styles);
styles = styles | NumberStyles.AllowTrailingSign;
CallTryParse(numericString, styles);
numericString = "$10634";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
styles = NumberStyles.Integer | NumberStyles.AllowCurrencySymbol;
CallTryParse(numericString, styles);
numericString = "10345.00";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "10345.72";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "22,593";
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)
{
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 success = Int64.TryParse(stringToConvert, styles,
provider, out long number);
if (success)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
else
Console.WriteLine("Attempted conversion of '{0}' failed.",
Convert.ToString(stringToConvert));
}
}
輸出:
Converted '106779' to 106779. Attempted conversion of '-30677' failed. Converted '-30677' to -30677. Attempted conversion of '301677-' failed. Converted '301677-' to -301677. Attempted conversion of '$10634' failed. Converted '$10634' to 10634. Converted '10345.00' to 10345. Attempted conversion of '10345.72' failed. Converted '22,593' to 22593. Attempted conversion of '12E-01' failed. Converted '12E03' to 12000. Converted '80c1' to 32961. Attempted conversion of '0x80C1' failed.