本文整理匯總了C#中System.Int64.Parse方法的典型用法代碼示例。如果您正苦於以下問題:C# Int64.Parse方法的具體用法?C# Int64.Parse怎麽用?C# Int64.Parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Int64
的用法示例。
在下文中一共展示了Int64.Parse方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.Globalization;
public class ParseInt64
{
public static void Main()
{
Convert("12,000", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("en-GB"));
Convert("12,000", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("fr-FR"));
Convert("12,000", NumberStyles.Float, new CultureInfo("en-US"));
Convert("12 425,00", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("sv-SE"));
Convert("12,425.00", NumberStyles.Float | NumberStyles.AllowThousands,
NumberFormatInfo.InvariantInfo);
Convert("631,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
new CultureInfo("fr-FR"));
Convert("631,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
new CultureInfo("en-US"));
Convert("631,900", 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);
}
}
}
// This example displays the following output to the console:
// Converted '12,000' to 12000.
// Converted '12,000' to 12.
// Unable to convert '12,000'.
// Converted '12 425,00' to 12425.
// Converted '12,425.00' to 12425.
// '631,900' is out of range of the Int64 type.
// Unable to convert '631,900'.
// Converted '631,900' to 631900.
示例2: Main
//引入命名空間
using System;
using System.Globalization;
public class ParseInt32
{
public static void Main()
{
Convert("104.0", NumberStyles.AllowDecimalPoint);
Convert("104.9", NumberStyles.AllowDecimalPoint);
Convert (" 106034", NumberStyles.None);
Convert(" $17,198,064.42", NumberStyles.AllowCurrencySymbol |
NumberStyles.Number);
Convert(" $17,198,064.00", NumberStyles.AllowCurrencySymbol |
NumberStyles.Number);
Convert("103E06", NumberStyles.AllowExponent);
Convert("1200E-02", NumberStyles.AllowExponent);
Convert("1200E-03", NumberStyles.AllowExponent);
Convert("-1,345,791", NumberStyles.AllowThousands);
Convert("(1,345,791)", NumberStyles.AllowThousands |
NumberStyles.AllowParentheses);
Convert("FFCA00A0", NumberStyles.HexNumber);
Convert("0xFFCA00A0", NumberStyles.HexNumber);
}
private static void Convert(string value, NumberStyles style)
{
try
{
long number = Int64.Parse(value, style);
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);
}
}
}
輸出:
Converted '104.0' to 104. '104.9' is out of range of the Int64 type. Unable to convert ' 106034'. ' $17,198,064.42' is out of range of the Int64 type. Converted ' $17,198,064.00' to 17198064. Converted '103E06' to 103000000. Converted '1200E-02' to 12. '1200E-03' is out of range of the Int64 type. Unable to convert '-1,345,791'. Converted '(1,345,791)' to -1345791. Converted 'FFCA00A0' to 4291428512. Unable to convert '0xFFCA00A0'.
示例3: Main
//引入命名空間
using System;
public class ParseInt64
{
public static void Main()
{
Convert(" 179042 ");
Convert(" -2041326 ");
Convert(" +8091522 ");
Convert(" 1064.0 ");
Convert(" 178.3");
Convert(String.Empty);
Convert(((decimal) Int64.MaxValue) + 1.ToString());
}
private static void Convert(string value)
{
try
{
long number = Int64.Parse(value);
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.", value);
}
}
}
// This example displays the following output to the console:
// Converted ' 179042 ' to 179042.
// Converted ' -2041326 ' to -2041326.
// Converted ' +8091522 ' to 8091522.
// Unable to convert ' 1064.0 '.
// Unable to convert ' 178.3'.
// Unable to convert ''.
// '92233720368547758071' is out of range.
示例4: OkToLong_Click
protected void OkToLong_Click(object sender, EventArgs e)
{
string locale;
long number;
CultureInfo culture;
// Return if string is empty
if (String.IsNullOrEmpty(this.inputNumber.Text))
return;
// Get locale of web request to determine possible format of number
if (Request.UserLanguages.Length == 0)
return;
locale = Request.UserLanguages[0];
if (String.IsNullOrEmpty(locale))
return;
// Instantiate CultureInfo object for the user's locale
culture = new CultureInfo(locale);
// Convert user input from a string to a number
try
{
number = Int64.Parse(this.inputNumber.Text, culture.NumberFormat);
}
catch (FormatException)
{
return;
}
catch (Exception)
{
return;
}
// Output number to label on web form
this.outputNumber.Text = "Number is " + number.ToString();
}