本文整理匯總了C#中System.Int32.Parse方法的典型用法代碼示例。如果您正苦於以下問題:C# Int32.Parse方法的具體用法?C# Int32.Parse怎麽用?C# Int32.Parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Int32
的用法示例。
在下文中一共展示了Int32.Parse方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.Globalization;
public class ParseInt32
{
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
{
int number = Int32.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 Int32 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 Int32 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(" $17,198,064.42", NumberStyles.AllowCurrencySymbol |
NumberStyles.Number);
Convert("103E06", NumberStyles.AllowExponent);
Convert("-1,345,791", NumberStyles.AllowThousands);
Convert("(1,345,791)", NumberStyles.AllowThousands |
NumberStyles.AllowParentheses);
}
private static void Convert(string value, NumberStyles style)
{
try
{
int number = Int32.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 Int32 type.", value);
}
}
}
輸出:
Converted '104.0' to 104. '104.9' is out of range of the Int32 type. ' $17,198,064.42' is out of range of the Int32 type. Converted '103E06' to 103000000. Unable to convert '-1,345,791'. Converted '(1,345,791)' to -1345791.
示例3: Main
//引入命名空間
using System;
public class Example
{
public static void Main()
{
string[] values = { "+13230", "-0", "1,390,146", "$190,235,421,127",
"0xFA1B", "163042", "-10", "007", "2147483647",
"2147483648", "16e07", "134985.0", "-12034",
"-2147483648", "-2147483649" };
foreach (string value in values)
{
try {
int number = Int32.Parse(value);
Console.WriteLine("{0} --> {1}", value, number);
}
catch (FormatException) {
Console.WriteLine("{0}: Bad Format", value);
}
catch (OverflowException) {
Console.WriteLine("{0}: Overflow", value);
}
}
}
}
輸出:
+13230 --> 13230 -0 --> 0 1,390,146: Bad Format $190,235,421,127: Bad Format 0xFA1B: Bad Format 163042 --> 163042 -10 --> -10 007 --> 7 2147483647 --> 2147483647 2147483648: Overflow 16e07: Bad Format 134985.0: Bad Format -12034 --> -12034 -2147483648 --> -2147483648 -2147483649: Overflow
示例4: OkToInteger_Click
protected void OkToInteger_Click(object sender, EventArgs e)
{
string locale;
int 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 = Int32.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();
}
示例5: Int32.Parse(String value)
//引入命名空間
using System;
class MainClass
{
public static void Main(string[] args)
{
int myInt = int.Parse("8");
Console.WriteLine("-> Value of myInt: {0}", myInt);
}
}