本文整理匯總了C#中System.Single.Parse方法的典型用法代碼示例。如果您正苦於以下問題:C# Single.Parse方法的具體用法?C# Single.Parse怎麽用?C# Single.Parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Single
的用法示例。
在下文中一共展示了Single.Parse方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
// Define an array of string values.
string[] values = { " 987.654E-2", " 987,654E-2", "(98765,43210)",
"9,876,543.210", "9.876.543,210", "98_76_54_32,19" };
// Create a custom culture based on the invariant culture.
CultureInfo ci = new CultureInfo("");
ci.NumberFormat.NumberGroupSizes = new int[] { 2 };
ci.NumberFormat.NumberGroupSeparator = "_";
// Define an array of format providers.
CultureInfo[] providers = { new CultureInfo("en-US"),
new CultureInfo("nl-NL"), ci };
// Define an array of styles.
NumberStyles[] styles = { NumberStyles.Currency, NumberStyles.Float };
// Iterate the array of format providers.
foreach (CultureInfo provider in providers)
{
Console.WriteLine("Parsing using the {0} culture:",
provider.Name == String.Empty ? "Invariant" : provider.Name);
// Parse each element in the array of string values.
foreach (string value in values)
{
foreach (NumberStyles style in styles)
{
try {
float number = Single.Parse(value, style, provider);
Console.WriteLine(" {0} ({1}) -> {2}",
value, style, number);
}
catch (FormatException) {
Console.WriteLine(" '{0}' is invalid using {1}.", value, style);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is out of the range of a Single.", value);
}
}
}
Console.WriteLine();
}
}
}
輸出:
Parsing using the en-US culture: The format of // 987.654E-2// is invalid. The format of // 987,654E-2// is invalid. (98765,43210) (Currency) -> -9.876543E+09 9,876,543.210 (Currency) -> 9876543 The format of '9.876.543,210// is invalid. The format of '98_76_54_32,19// is invalid. Parsing using the nl-NL culture: The format of // 987.654E-2// is invalid. The format of // 987,654E-2// is invalid. (98765,43210) (Currency) -> -98765.43 The format of '9,876,543.210// is invalid. 9.876.543,210 (Currency) -> 9876543 The format of '98_76_54_32,19// is invalid. Parsing using the Invariant culture: The format of // 987.654E-2// is invalid. The format of // 987,654E-2// is invalid. (98765,43210) (Currency) -> -9.876543E+09 9,876,543.210 (Currency) -> 9876543 The format of '9.876.543,210// is invalid. 98_76_54_32,19 (Currency) -> 9.876543E+09
示例2: OkToSingle_Click
protected void OkToSingle_Click(object sender, EventArgs e)
{
string locale;
float 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 = Single.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();
}
示例3: Main
//引入命名空間
using System;
public class Example
{
public static void Main()
{
string[] values = { "100", "(100)", "-123,456,789", "123.45e+6",
"+500", "5e2", "3.1416", "600.", "-.123",
"-Infinity", "-1E-16", Double.MaxValue.ToString(),
Single.MinValue.ToString(), String.Empty };
foreach (string value in values)
{
try {
float number = Single.Parse(value);
Console.WriteLine("{0} -> {1}", value, number);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in a valid format.", value);
}
catch (OverflowException) {
Console.WriteLine("{0} is outside the range of a Single.", value);
}
}
}
}
輸出:
100 -> 100 '(100)' is not in a valid format. -123,456,789 -> -1.234568E+08 123.45e+6 -> 1.2345E+08 +500 -> 500 5e2 -> 500 3.1416 -> 3.1416 600. -> 600 -.123 -> -0.123 -Infinity -> -Infinity -1E-16 -> -1E-16 1.79769313486232E+308 is outside the range of a Single. -3.402823E+38 -> -3.402823E+38 '' is not in a valid format.
示例4: Main
//引入命名空間
using System;
using System.Globalization;
using System.Threading;
public class ParseString
{
public static void Main()
{
// Set current thread culture to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
string value;
NumberStyles styles;
// Parse a string in exponential notation with only the AllowExponent flag.
value = "-1.063E-02";
styles = NumberStyles.AllowExponent;
ShowNumericValue(value, styles);
// Parse a string in exponential notation
// with the AllowExponent and Number flags.
styles = NumberStyles.AllowExponent | NumberStyles.Number;
ShowNumericValue(value, styles);
// Parse a currency value with leading and trailing white space, and
// white space after the U.S. currency symbol.
value = " $ 6,164.3299 ";
styles = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
ShowNumericValue(value, styles);
// Parse negative value with thousands separator and decimal.
value = "(4,320.64)";
styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
NumberStyles.Float;
ShowNumericValue(value, styles);
styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
NumberStyles.Float | NumberStyles.AllowThousands;
ShowNumericValue(value, styles);
}
private static void ShowNumericValue(string value, NumberStyles styles)
{
Single number;
try
{
number = Single.Parse(value, styles);
Console.WriteLine("Converted '{0}' using {1} to {2}.",
value, styles.ToString(), number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}' with styles {1}.",
value, styles.ToString());
}
Console.WriteLine();
}
}
輸出:
Unable to parse '-1.063E-02' with styles AllowExponent. Converted '-1.063E-02' using AllowTrailingSign, AllowThousands, Float to -0.01063. Converted ' $ 6,164.3299 ' using Number, AllowCurrencySymbol to 6164.3299. Unable to parse '(4,320.64)' with styles AllowTrailingSign, AllowParentheses, Float. Converted '(4,320.64)' using AllowTrailingSign, AllowParentheses, AllowThousands, Float to -4320.64.
示例5: Main
//引入命名空間
using System;
using System.Data;
class Class1{
static void Main(string[] args){
string IsNotNum = "111west";
string IsNum = " +111 ";
string IsFloat = " 23.11 ";
string IsExp = " +23 e+11 ";
Console.WriteLine(int.Parse(IsNum));
Console.WriteLine(float.Parse(IsNum)); // 111
Console.WriteLine(float.Parse(IsFloat)); // 23.11
//Console.WriteLine(float.Parse(IsExp)); // throws
try
{
Console.WriteLine(int.Parse(IsNotNum));
}
catch (FormatException e)
{
Console.WriteLine("Not a numeric value: {0}", e.ToString()); // throws
}
}
}