本文整理汇总了C#中System.UInt32.Parse方法的典型用法代码示例。如果您正苦于以下问题:C# UInt32.Parse方法的具体用法?C# UInt32.Parse怎么用?C# UInt32.Parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了UInt32.Parse方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] cultureNames= { "en-US", "fr-FR" };
NumberStyles[] styles= { NumberStyles.Integer,
NumberStyles.Integer | NumberStyles.AllowDecimalPoint };
string[] values = { "170209", "+170209.0", "+170209,0", "-103214.00",
"-103214,00", "104561.1", "104561,1" };
// Parse strings using each culture
foreach (string cultureName in cultureNames)
{
CultureInfo ci = new CultureInfo(cultureName);
Console.WriteLine("Parsing strings using the {0} culture",
ci.DisplayName);
// Use each style.
foreach (NumberStyles style in styles)
{
Console.WriteLine(" Style: {0}", style.ToString());
// Parse each numeric string.
foreach (string value in values)
{
try {
Console.WriteLine(" Converted '{0}' to {1}.", value,
UInt32.Parse(value, style, ci));
}
catch (FormatException) {
Console.WriteLine(" Unable to parse '{0}'.", value);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is out of range of the UInt32 type.",
value);
}
}
}
}
}
}
输出:
Parsing strings using the English (United States) culture Style: Integer Converted '170209' to 170209. Unable to parse '+170209.0'. Unable to parse '+170209,0'. Unable to parse '-103214.00'. Unable to parse '-103214,00'. Unable to parse '104561.1'. Unable to parse '104561,1'. Style: Integer, AllowDecimalPoint Converted '170209' to 170209. Converted '+170209.0' to 170209. Unable to parse '+170209,0'. '-103214.00' is out of range of the UInt32 type. Unable to parse '-103214,00'. '104561.1' is out of range of the UInt32 type. Unable to parse '104561,1'. Parsing strings using the French (France) culture Style: Integer Converted '170209' to 170209. Unable to parse '+170209.0'. Unable to parse '+170209,0'. Unable to parse '-103214.00'. Unable to parse '-103214,00'. Unable to parse '104561.1'. Unable to parse '104561,1'. Style: Integer, AllowDecimalPoint Converted '170209' to 170209. Unable to parse '+170209.0'. Converted '+170209,0' to 170209. Unable to parse '-103214.00'. '-103214,00' is out of range of the UInt32 type. Unable to parse '104561.1'. '104561,1' is out of range of the UInt32 type.
示例2: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] values= { " 214309 ", "1,064,181", "(0)", "10241+", " + 21499 ",
" +21499 ", "122153.00", "1e03ff", "91300.0e-2" };
NumberStyles whitespace = NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite;
NumberStyles[] styles= { NumberStyles.None, whitespace,
NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign | whitespace,
NumberStyles.AllowThousands | NumberStyles.AllowCurrencySymbol,
NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint };
// Attempt to convert each number using each style combination.
foreach (string value in values)
{
Console.WriteLine("Attempting to convert '{0}':", value);
foreach (NumberStyles style in styles)
{
try {
uint number = UInt32.Parse(value, style);
Console.WriteLine(" {0}: {1}", style, number);
}
catch (FormatException) {
Console.WriteLine(" {0}: Bad Format", style);
}
catch (OverflowException)
{
Console.WriteLine(" {0}: Overflow", value);
}
}
Console.WriteLine();
}
}
}
输出:
Attempting to convert ' 214309 ': None: Bad Format AllowLeadingWhite, AllowTrailingWhite: 214309 Integer, AllowTrailingSign: 214309 AllowThousands, AllowCurrencySymbol: Bad Format AllowDecimalPoint, AllowExponent: Bad Format Attempting to convert '1,064,181': None: Bad Format AllowLeadingWhite, AllowTrailingWhite: Bad Format Integer, AllowTrailingSign: Bad Format AllowThousands, AllowCurrencySymbol: 1064181 AllowDecimalPoint, AllowExponent: Bad Format Attempting to convert '(0)': None: Bad Format AllowLeadingWhite, AllowTrailingWhite: Bad Format Integer, AllowTrailingSign: Bad Format AllowThousands, AllowCurrencySymbol: Bad Format AllowDecimalPoint, AllowExponent: Bad Format Attempting to convert '10241+': None: Bad Format AllowLeadingWhite, AllowTrailingWhite: Bad Format Integer, AllowTrailingSign: 10241 AllowThousands, AllowCurrencySymbol: Bad Format AllowDecimalPoint, AllowExponent: Bad Format Attempting to convert ' + 21499 ': None: Bad Format AllowLeadingWhite, AllowTrailingWhite: Bad Format Integer, AllowTrailingSign: Bad Format AllowThousands, AllowCurrencySymbol: Bad Format AllowDecimalPoint, AllowExponent: Bad Format Attempting to convert ' +21499 ': None: Bad Format AllowLeadingWhite, AllowTrailingWhite: Bad Format Integer, AllowTrailingSign: 21499 AllowThousands, AllowCurrencySymbol: Bad Format AllowDecimalPoint, AllowExponent: Bad Format Attempting to convert '122153.00': None: Bad Format AllowLeadingWhite, AllowTrailingWhite: Bad Format Integer, AllowTrailingSign: Bad Format AllowThousands, AllowCurrencySymbol: Bad Format AllowDecimalPoint, AllowExponent: 122153 Attempting to convert '1e03ff': None: Bad Format AllowLeadingWhite, AllowTrailingWhite: Bad Format Integer, AllowTrailingSign: Bad Format AllowThousands, AllowCurrencySymbol: Bad Format AllowDecimalPoint, AllowExponent: Bad Format Attempting to convert '91300.0e-2': None: Bad Format AllowLeadingWhite, AllowTrailingWhite: Bad Format Integer, AllowTrailingSign: Bad Format AllowThousands, AllowCurrencySymbol: Bad Format AllowDecimalPoint, AllowExponent: 913
示例3: foreach
string[] values = { "+13230", "-0", "1,390,146", "$190,235,421,127",
"0xFA1B", "163042", "-10", "2147483648",
"14065839182", "16e07", "134985.0", "-12034" };
foreach (string value in values)
{
try {
uint number = UInt32.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: Overflow 2147483648 --> 2147483648 14065839182: Overflow 16e07: Bad Format 134985.0: Bad Format -12034: Overflow
示例4: OkToUInteger_Click
protected void OkToUInteger_Click(object sender, EventArgs e)
{
string locale;
uint 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 = UInt32.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();
}