Single.Parse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將數位的字串表示轉換為其對等的單精度浮點數。
多載
| 名稱 | Description |
|---|---|
| Parse(String) |
將數位的字串表示轉換為其對等的單精度浮點數。 |
| Parse(ReadOnlySpan<Byte>, IFormatProvider) |
將UTF-8字元的範圍剖析為值。 |
| Parse(ReadOnlySpan<Char>, IFormatProvider) |
將字元範圍剖析為值。 |
| Parse(String, NumberStyles) |
將指定樣式中數位的字串表示轉換為其單精度浮點數對等專案。 |
| Parse(String, IFormatProvider) |
將指定之特定文化特性格式之數位的字串表示轉換為其對等的單精度浮點數。 |
| Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
將UTF-8字元的範圍剖析為值。 |
| Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
將字元範圍,其中包含指定樣式和特定文化特性格式之數位的字串表示,轉換為其單精度浮點數對等專案。 |
| Parse(String, NumberStyles, IFormatProvider) |
將指定樣式和特定文化特性格式之數位的字串表示轉換為其單精度浮點數對等專案。 |
備註
在 .NET Core 3.0 及之後版本中,過大無法表示的數值會被四捨五入到 PositiveInfinity 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
Parse(String)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
將數位的字串表示轉換為其對等的單精度浮點數。
public:
static float Parse(System::String ^ s);
public static float Parse(string s);
static member Parse : string -> single
Public Shared Function Parse (s As String) As Single
參數
- s
- String
字串,包含要轉換的數位。
傳回
一個與中 s指定的數值或符號等價的單精度浮點數。
例外狀況
s 為 null。
s 不代表一個有效格式的數字。
.NET Framework 及 .NET Core 2.2 及更早版本: s 代表的數字小於 Single.MinValue ,或大於 Single.MaxValue。
範例
以下範例使用此 Parse(String) 方法將一組字串轉換為等價 Single 值。
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);
}
}
}
}
// The example displays the following output:
// 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.
open System
let values =
[| "100"; "(100)"; "-123,456,789"; "123.45e+6"
"+500"; "5e2"; "3.1416"; "600."; "-.123"
"-Infinity"; "-1E-16"; string Double.MaxValue
string Single.MinValue; String.Empty |]
for value in values do
try
let number = Single.Parse value
printfn $"{value} -> {number}"
with
| :? FormatException ->
printfn $"'{value}' is not in a valid format."
| :? OverflowException ->
printfn $"{value} is outside the range of a Single."
// The example displays the following output:
// 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.
Module Example
Public Sub Main()
Dim values() As String = { "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 }
For Each value As String In values
Try
Dim number As Single = Single.Parse(value)
Console.WriteLine("{0} -> {1}", value, number)
Catch e As FormatException
Console.WriteLine("'{0}' is not in a valid format.", value)
Catch e As OverflowException
Console.WriteLine("{0} is outside the range of a Single.", value)
End Try
Next
End Sub
End Module
' The example displays the following output:
' 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.
備註
在 .NET Core 3.0 及之後版本中,過大無法表示的數值會被四捨五入到 PositiveInfinity 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
參數s可包含當前文化的 PositiveInfinitySymbol、 或NegativeInfinitySymbolNaNSymbol符號。 此字串比較在 .NET Core 3.0 及以後版本中不區分大小寫,但在先前版本(包括 .NET Framework)中則不區分大小寫。 參數 s 也可以是如下形式的字串:
[ws][標誌][整數數字[,]]整數數字[.[小數數字]][e[符號]指數數字][ws]
方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。
| 元素 | 描述 |
|---|---|
| ws | 一系列的空格符。 |
| 簽署 | 負號符號或正負號符號。 有效的符號特徵是由 NumberFormatInfo.NegativeSign 當前文化的屬性 NumberFormatInfo.PositiveSign 決定的。 只能使用前置符號。 |
| 整數數字 | 一系列數位,範圍從 0 到 9,指定數位的整數部分。 整 數數字 的連續可以用分隔符號來劃分。 例如,在某些文化特性中,逗號 (,) 會分隔數千個群組。 如果字串包含小數位元元素,整數數字元素可以不存在。 |
| , | 特定文化特性的千位分隔符符號。 |
| . | 特定文化特性的小數點符號。 |
| 小數數字 | 一系列數位,範圍從 0 到 9,指定數位的小數部分。 |
| E | “e” 或 “E” 字元,表示值是以指數(科學)表示法表示。 |
| 指數位數 | 指定指數的一系列數位範圍從 0 到 9。 |
s參數是透過 和 NumberStyles.AllowThousands 旗標的組合NumberStyles.Float來解釋的。 這表示允許空格符和千位分隔符,但貨幣符號則不允許。 要明確定義 中可能存在 s的元素(如貨幣符號、數千個分隔符和空白),可以使用 Parse(String, NumberStyles) 方法過載。
s參數是利用物件格式資訊解析,該物件已NumberFormatInfo初始化為當前系統文化。 如需詳細資訊,請參閱CurrentInfo。 要利用特定文化的格式資訊解析字串,請使用 Parse(String, IFormatProvider) or Parse(String, NumberStyles, IFormatProvider) 方法。
通常,如果你傳 Parse 給方法一個由呼叫 ToString 該方法所產生的字串, Single 原始值就會被回傳。 不過,由於精確度遺失,值可能不相等。
如果 s 超出資料Single型態範圍,該方法會在 .NET Framework 及 .NET Core 2.2 及更早版本中拋出一個 。OverflowException 在 .NET Core 3.0 及更新版本中,當 小於 且 Single.PositiveInfinitySingle.MinValues 大於 Single.MaxValue時,會回傳 Single.NegativeInfinity 。s
若在解析操作中遇到參數分 s 隔符,且適用的貨幣或數字分隔符相同,則解析操作假設分隔符為十進位分隔符而非群分隔符。 欲了解更多分離子資訊,請參見 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparatorNumberGroupSeparator及 。
另請參閱
適用於
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
將UTF-8字元的範圍剖析為值。
public:
static float Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<float>::Parse;
public static float Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> single
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Single
參數
- utf8Text
- ReadOnlySpan<Byte>
要剖析的UTF-8字元範圍。
- provider
- IFormatProvider
一個提供關於 utf8Text的文化特定格式資訊的物件。
傳回
解析 的結果 utf8Text。
實作
適用於
Parse(ReadOnlySpan<Char>, IFormatProvider)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
將字元範圍剖析為值。
public:
static float Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<float>::Parse;
public static float Parse(ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> single
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Single
參數
- s
- ReadOnlySpan<Char>
要剖析的字元範圍。
- provider
- IFormatProvider
一個提供關於 s的文化特定格式資訊的物件。
傳回
解析 的結果 s。
實作
適用於
Parse(String, NumberStyles)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
將指定樣式中數位的字串表示轉換為其單精度浮點數對等專案。
public:
static float Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static float Parse(string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> single
Public Shared Function Parse (s As String, style As NumberStyles) As Single
參數
- s
- String
字串,包含要轉換的數位。
- style
- NumberStyles
一個位元組合的列舉值,表示可能存在 s於 中的樣式元素。 典型的指定值是 Float 與 AllowThousands結合。
傳回
一個單精度浮點數,等同於 中 s指定的數值或符號。
例外狀況
s 為 null。
s 不是有效格式的數字。
.NET Framework 及 .NET Core 2.2 及更早版本: s 代表的數字小於 Single.MinValue 或大於 Single.MaxValue 的數字。
範例
以下範例使用該 Parse(String, NumberStyles) 方法解析數值的 Single 字串表示。 範例中使用了 en-US 文化的格式資訊。
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();
}
}
// The example displays the following output to the console:
// 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.
open System
open System.Globalization
open System.Threading
let showNumericValue value (styles: NumberStyles) =
try
let number = Single.Parse(value, styles)
printfn $"Converted '{value}' using {styles} to {number}."
with :? FormatException ->
printfn $"Unable to parse '{value}' with styles {styles}."
printfn ""
[<EntryPoint>]
let main _ =
// Set current thread culture to en-US.
Thread.CurrentThread.CurrentCulture <- CultureInfo.CreateSpecificCulture "en-US"
// Parse a string in exponential notation with only the AllowExponent flag.
let value = "-1.063E-02"
let styles = NumberStyles.AllowExponent
showNumericValue value styles
// Parse a string in exponential notation
// with the AllowExponent and Number flags.
let 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.
let value = " $ 6,164.3299 "
let styles = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol
showNumericValue value styles
// Parse negative value with thousands separator and decimal.
let value = "(4,320.64)"
let styles = NumberStyles.AllowParentheses ||| NumberStyles.AllowTrailingSign ||| NumberStyles.Float
showNumericValue value styles
let styles = NumberStyles.AllowParentheses ||| NumberStyles.AllowTrailingSign ||| NumberStyles.Float ||| NumberStyles.AllowThousands
showNumericValue value styles
0
// The example displays the following output to the console:
// 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.
Imports System.Globalization
Imports System.Threading
Module ParseStrings
Public Sub Main()
' Set current thread culture to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Dim value As String
Dim styles As NumberStyles
' 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 Or 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 Or NumberStyles.AllowCurrencySymbol
ShowNumericValue(value, styles)
' Parse negative value with thousands separator and decimal.
value = "(4,320.64)"
styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
Or NumberStyles.Float
ShowNumericValue(value, styles)
styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
Or NumberStyles.Float Or NumberStyles.AllowThousands
ShowNumericValue(value, styles)
End Sub
Private Sub ShowNumericValue(value As String, styles As NumberStyles)
Dim number As Single
Try
number = Single.Parse(value, styles)
Console.WriteLine("Converted '{0}' using {1} to {2}.", _
value, styles.ToString(), number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}' with styles {1}.", _
value, styles.ToString())
End Try
Console.WriteLine()
End Sub
End Module
' The example displays the following output to the console:
' 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.
備註
在 .NET Core 3.0 及之後版本中,過大無法表示的數值會被四捨五入到 PositiveInfinity 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
參數 style 定義了允許在解析操作中 s 成功使用風格元素(如空白、數千個分隔符和貨幣符號)。 它必須是列舉中多個位元旗 NumberStyles 標的組合。 以下 NumberStyles 成員不獲支援:
參數s可包含當前文化的 PositiveInfinitySymbol、 或NegativeInfinitySymbolNaNSymbol符號。 此字串比較在 .NET Core 3.0 及以後版本中不區分大小寫,但在先前版本(包括 .NET Framework)中則不區分大小寫。 根據 的 style值, s 參數也可以呈現如下形式:
[ws][$][符號][整數數字[,]]整數數字[。[小數數字]][E[符號]指數數字][ws]
方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。
WS 一連串空白字元。 如果包含該旗幟,空白可以出現在 的s開頭,如果包含該NumberStyles.AllowTrailingWhite旗,則可以出現在 的結尾styles。NumberStyles.AllowLeadingWhitestyle
$ 特定文化特性的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern當前文化的性質所定義。 當前s文化的貨幣符號可以出現style在包含NumberStyles.AllowCurrencySymbol國旗的情況下。
標誌 一個負符號符號(-)或一個正符號符號(+)。 如果包含NumberStyles.AllowLeadingSign旗幟,標誌可以出現在 的styles開頭,如果包含NumberStyles.AllowTrailingSign該旗幟,則可以出現在 的結尾sstyle。 若s包含旗標,style則可使用NumberStyles.AllowParentheses括號表示負值。
整數數字 一連串從0到9的數字,用來指定數字的整數部分。 如果字串包含小數位元元素,整數數字元素可以不存在。
,特定文化特性的群組分隔符。 當前文化的群組分隔符號可以出現在 style 如果s包含旗NumberStyles.AllowThousands幟
.
特定文化特性的小數點符號。 當前文化的小數點符號可以出現sstyle在包含NumberStyles.AllowDecimalPoint國旗的情況下。
小數數字 一連串從0到9的數字,用來表示數字的分數部分。 若style包含NumberStyles.AllowDecimalPoint旗幟,則可出現s小數數字。
E “e” 或 “E” 字元,表示值是以指數(科學)表示法表示。 若value包含style旗標,參數NumberStyles.AllowExponent可用指數符號表示數字。
指數位數 一連串從0到9的數字,用來指定一個指數。
注意
任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。
只有數字的字串(對應 NumberStyles.None 於樣式)若在該類型範圍內 Single ,則能成功解析。 剩餘 System.Globalization.NumberStyles 成員控制輸入字串中可能存在但不必須存在的元素。 下表說明個別 NumberStyles 旗標如何影響可能存在於 s中的元素。
| NumberStyles 值 | 除了數字外,允許加入 s 的元素 |
|---|---|
| None | 只有 整數數字 元素。 |
| AllowDecimalPoint | 小數點(.)和 小數數字 元素。 |
| AllowExponent | 表示指數表示法的 「e」 或 「E」 字元。 此旗標本身支援數字 E數字;需要額外旗標才能成功解析包含正負符號及小數點符號等元素的字串。 |
| AllowLeadingWhite | 開頭s的 w 元素。 |
| AllowTrailingWhite | 末尾的 s 元素。 |
| AllowLeadingSign | 符號 元素位於 的開頭 s。 |
| AllowTrailingSign | 符號 元素位於 的結尾 s。 |
| AllowParentheses | 符號 元素以 括號形式包含數值。 |
| AllowThousands | 千位分隔符 (,) 元素。 |
| AllowCurrencySymbol | currency ($) 元素。 |
| Currency | 所有元素。 然而,無法 s 表示十六進位數或指數符號中的數字。 |
| Float | ws 元素位於 的s開頭或結尾,符號開頭s的符號,以及小數點符號(.)。 參數 s 也可以使用指數符號。 |
| Number | 、wssign、千分符(,)和小數點(.)元素。 |
| Any | 所有元素。 然而,無法 s 代表十六進位數。 |
例如 s 「100」、「-123,456,789」、「123.45e+6」、「+500」、「5e2」、「3.1416」、「600.」、「-.123」和「-無限」。
s參數是利用為當前系統文化初始化的物件格式資訊NumberFormatInfo進行解析。 若要指定用於解析操作的格式資訊文化,請呼叫 overload Parse(String, NumberStyles, IFormatProvider) 。
通常,如果你傳 Parse 給方法一個由呼叫 ToString 該方法所產生的字串, Single 原始值就會被回傳。 不過,由於精確度遺失,值可能不相等。
如果 s 超出資料Single型態範圍,該方法會在 .NET Framework 及 .NET Core 2.2 及更早版本中拋出一個 。OverflowException 在 .NET Core 3.0 及更新版本中,當 小於 且 Single.PositiveInfinitySingle.MinValues 大於 Single.MaxValue時,會回傳 Single.NegativeInfinity 。s
若在解析操作中遇到參數分 s 隔符,且適用的貨幣或數字分隔符相同,則解析操作假設分隔符為十進位分隔符而非群分隔符。 欲了解更多分離子資訊,請參見 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparatorNumberGroupSeparator及 。
另請參閱
適用於
Parse(String, IFormatProvider)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
將指定之特定文化特性格式之數位的字串表示轉換為其對等的單精度浮點數。
public:
static float Parse(System::String ^ s, IFormatProvider ^ provider);
public:
static float Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<float>::Parse;
public static float Parse(string s, IFormatProvider provider);
public static float Parse(string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> single
Public Shared Function Parse (s As String, provider As IFormatProvider) As Single
參數
- s
- String
字串,包含要轉換的數位。
- provider
- IFormatProvider
一個提供關於 s的文化特定格式資訊的物件。
傳回
一個與中 s指定的數值或符號等價的單精度浮點數。
實作
例外狀況
s 為 null。
s 不代表一個有效格式的數字。
.NET Framework 及 .NET Core 2.2 及更早版本: s 代表的數字小於 Single.MinValue ,或大於 Single.MaxValue。
範例
下列範例是 Web 窗體的按鈕點選事件處理程式。 它利用屬性 HttpRequest.UserLanguages 回傳的陣列來決定使用者的所在位置。 接著它實例化一個 CultureInfo 對應該地點的物件。 NumberFormatInfo屬於該CultureInfo物件的物件會被傳給Parse(String, IFormatProvider)將使用者輸入轉換成Single值的方法。
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();
}
Protected Sub OkToSingle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToSingle.Click
Dim locale As String
Dim culture As CultureInfo
Dim number As Single
' Return if string is empty
If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub
' Get locale of web request to determine possible format of number
If Request.UserLanguages.Length = 0 Then Exit Sub
locale = Request.UserLanguages(0)
If String.IsNullOrEmpty(locale) Then Exit Sub
' 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(Me.inputNumber.Text, culture.NumberFormat)
Catch ex As FormatException
Exit Sub
Catch ex As OverflowException
Exit Sub
End Try
' Output number to label on web form
Me.outputNumber.Text = "Number is " & number.ToString()
End Sub
備註
在 .NET Core 3.0 及之後版本中,過大無法表示的數值會被四捨五入到 PositiveInfinity 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
這種過載通常用來將可以多種格式化的文字轉換為一個 Single 值。 例如,它可以用來將使用者輸入的文字轉換成 HTML 文字框,轉換為數值。
s參數是透過 和 NumberStyles.AllowThousands 旗標的組合NumberStyles.Float來解釋的。 參數 s 可包含 NumberFormatInfo.PositiveInfinitySymbol、 , NumberFormatInfo.NegativeInfinitySymbol或 NumberFormatInfo.NaNSymbol 由 指定 provider為文化的符號。 此字串比較在 .NET Core 3.0 及以後版本中不區分大小寫,但在先前版本(包括 .NET Framework)中則不區分大小寫。 參數 s 可包含如下形式的字串:
[ws][標誌]整數數字[.[小數數字]][E[符號]指數數字][ws]
選擇性專案會以方括弧 ([ 和 ]) 框住。 包含「digits」 一詞的元素包含一系列從 0 到 9 的數位字元。
| 元素 | 描述 |
|---|---|
| ws | 一系列空格符。 |
| 簽署 | 負號符號 (-) 或正負號符號 (+)。 |
| 整數數字 | 一系列數位,範圍從 0 到 9,指定數位的整數部分。 整 數數字 的連續可以用分隔符號來劃分。 例如,在某些文化特性中,逗號 (,) 會分隔數千個群組。 如果字串包含小數位元元素,整數數字元素可以不存在。 |
| . | 特定文化特性的小數點符號。 |
| 小數數字 | 一系列數位,範圍從 0 到 9,指定數位的小數部分。 |
| E | “e” 或 “E” 字元,表示值是以指數(科學)表示法表示。 |
| 指數位數 | 指定指數的一系列數位範圍從 0 到 9。 |
欲了解更多關於數字格式的資訊,請參閱 格式類型 主題。
參數 provider 是一個 IFormatProvider 實作,其 GetFormat 方法回傳 NumberFormatInfo 一個物件,提供特定文化的格式資訊。 當 Parse(String, IFormatProvider) 方法被呼叫時,它會呼叫 provider 參數的方法 GetFormat ,並傳入 Type 一個代表該型別的 NumberFormatInfo 物件。 接著,該 GetFormat 方法會回傳 NumberFormatInfo 提供參數格式 s 資訊的物件。 有三種方式可以利用該 provider 參數為解析操作提供自訂格式資訊:
你可以傳遞 CultureInfo 一個代表文化的物件,提供格式資訊。 其 GetFormat 方法回傳 NumberFormatInfo 提供該文化數值格式資訊的物件。
你可以傳遞提供數字格式資訊的實體 NumberFormatInfo 物件。 (它的實作 GetFormat 只是回傳自己。)
你可以傳遞一個自訂物件來實作 IFormatProvider。 其 GetFormat 方法實例化並提供格式資訊的 NumberFormatInfo 物件。
若 provider 無法取得 is null 或 a NumberFormatInfo ,則會使用當前系統文化的格式資訊。
如果 s 超出資料Single型態範圍,該方法會在 .NET Framework 及 .NET Core 2.2 及更早版本中拋出一個 。OverflowException 在 .NET Core 3.0 及更新版本中,當 小於 且 Single.PositiveInfinitySingle.MinValues 大於 Single.MaxValue時,會回傳 Single.NegativeInfinity 。s
若在解析操作中遇到參數分 s 隔符,且適用的貨幣或數字分隔符相同,則解析操作假設分隔符為十進位分隔符而非群分隔符。 欲了解更多分離子資訊,請參見 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparatorNumberGroupSeparator及 。
例如 s 「100」、「-123,456,789」、「123.45e+6」、「+500」、「5e2」、「3.1416」、「600.」、「-.123」和「-無限」。
另請參閱
適用於
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
將UTF-8字元的範圍剖析為值。
public static float Parse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> single
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, Optional provider As IFormatProvider = Nothing) As Single
參數
- utf8Text
- ReadOnlySpan<Byte>
要剖析的UTF-8字元範圍。
- style
- NumberStyles
一個可以存在 utf8Text於 的數字樣式的位元組合。
- provider
- IFormatProvider
一個提供關於 utf8Text的文化特定格式資訊的物件。
傳回
解析 的結果 utf8Text。
實作
適用於
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
將字元範圍,其中包含指定樣式和特定文化特性格式之數位的字串表示,轉換為其單精度浮點數對等專案。
public static float Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, IFormatProvider? provider = default);
public static float Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> single
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, Optional provider As IFormatProvider = Nothing) As Single
參數
- s
- ReadOnlySpan<Char>
包含要轉換之數位的字元範圍。
- style
- NumberStyles
一個位元組合的列舉值,表示可能存在 s於 中的樣式元素。 典型的指定值是 Float 與 AllowThousands結合。
- provider
- IFormatProvider
一個提供關於 s的文化特定格式資訊的物件。
傳回
一個單精度浮點數,等同於 中 s指定的數值或符號。
實作
例外狀況
s 不代表數值。
備註
在 .NET Core 3.0 及之後版本中,過大無法表示的數值會被四捨五入到 PositiveInfinity 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
若 s 超出資料型別範圍Single,則若 s 小於 且 sSingle.MinValueSingle.PositiveInfinity 大於 Single.MaxValue,則回傳 。Single.NegativeInfinity
適用於
Parse(String, NumberStyles, IFormatProvider)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
將指定樣式和特定文化特性格式之數位的字串表示轉換為其單精度浮點數對等專案。
public:
static float Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
static float Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<float>::Parse;
public static float Parse(string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static float Parse(string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> single
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Single
參數
- s
- String
字串,包含要轉換的數位。
- style
- NumberStyles
一個位元組合的列舉值,表示可能存在 s於 中的樣式元素。 典型的指定值是 Float 與 AllowThousands結合。
- provider
- IFormatProvider
一個提供關於 s的文化特定格式資訊的物件。
傳回
一個與中 s指定的數值或符號等價的單精度浮點數。
實作
例外狀況
s 為 null。
s 不代表數值。
.NET Framework 及 .NET Core 2.2 及更早版本: s 代表的數字小於 Single.MinValue 或大於 Single.MaxValue 的數字。
範例
以下程式碼範例使用該 Parse(String, NumberStyles, IFormatProvider) 方法來解析數值的 Single 字串表示。 陣列中的每個字串皆依照 en-US、nl-NL及自訂文化的格式慣例進行解析。 自定義文化特性會將其群組分隔符符號定義為底線 (“_”),並將其群組大小定義為兩個。
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();
}
}
}
// The example displays the following output:
// Parsing using the en-US culture:
// ' 987.654E-2' is invalid using Currency.
// 987.654E-2 (Float) -> 9.87654
// ' 987,654E-2' is invalid using Currency.
// ' 987,654E-2' is invalid using Float.
// (98765,43210) (Currency) -> -9.876543E+09
// '(98765,43210)' is invalid using Float.
// 9,876,543.210 (Currency) -> 9876543
// '9,876,543.210' is invalid using Float.
// '9.876.543,210' is invalid using Currency.
// '9.876.543,210' is invalid using Float.
// '98_76_54_32,19' is invalid using Currency.
// '98_76_54_32,19' is invalid using Float.
//
// Parsing using the nl-NL culture:
// ' 987.654E-2' is invalid using Currency.
// ' 987.654E-2' is invalid using Float.
// ' 987,654E-2' is invalid using Currency.
// 987,654E-2 (Float) -> 9.87654
// (98765,43210) (Currency) -> -98765.43
// '(98765,43210)' is invalid using Float.
// '9,876,543.210' is invalid using Currency.
// '9,876,543.210' is invalid using Float.
// 9.876.543,210 (Currency) -> 9876543
// '9.876.543,210' is invalid using Float.
// '98_76_54_32,19' is invalid using Currency.
// '98_76_54_32,19' is invalid using Float.
//
// Parsing using the Invariant culture:
// ' 987.654E-2' is invalid using Currency.
// 987.654E-2 (Float) -> 9.87654
// ' 987,654E-2' is invalid using Currency.
// ' 987,654E-2' is invalid using Float.
// (98765,43210) (Currency) -> -9.876543E+09
// '(98765,43210)' is invalid using Float.
// 9,876,543.210 (Currency) -> 9876543
// '9,876,543.210' is invalid using Float.
// '9.876.543,210' is invalid using Currency.
// '9.876.543,210' is invalid using Float.
// 98_76_54_32,19 (Currency) -> 9.876543E+09
// '98_76_54_32,19' is invalid using Float.
open System
open System.Globalization
// Define a list of string values.
let 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.
let ci = CultureInfo ""
ci.NumberFormat.NumberGroupSizes <- [| 2 |]
ci.NumberFormat.NumberGroupSeparator <- "_"
// Define a list of format providers.
let providers =
[ CultureInfo "en-US"
CultureInfo "nl-NL"
ci ]
// Define a list of styles.
let styles = [ NumberStyles.Currency; NumberStyles.Float ]
// Iterate the list of format providers.
for provider in providers do
printfn $"""Parsing using the {if provider.Name = String.Empty then "Invariant" else provider.Name} culture:"""
// Parse each element in the array of string values.
for value in values do
for style in styles do
try
let number = Single.Parse(value, style, provider)
printfn $" {value} ({style}) -> {number}"
with
| :? FormatException ->
printfn $" '{value}' is invalid using {style}."
| :? OverflowException ->
printfn $" '{value}' is out of the range of a Single."
printfn ""
// The example displays the following output:
// Parsing using the en-US culture:
// ' 987.654E-2' is invalid using Currency.
// 987.654E-2 (Float) -> 9.87654
// ' 987,654E-2' is invalid using Currency.
// ' 987,654E-2' is invalid using Float.
// (98765,43210) (Currency) -> -9.876543E+09
// '(98765,43210)' is invalid using Float.
// 9,876,543.210 (Currency) -> 9876543
// '9,876,543.210' is invalid using Float.
// '9.876.543,210' is invalid using Currency.
// '9.876.543,210' is invalid using Float.
// '98_76_54_32,19' is invalid using Currency.
// '98_76_54_32,19' is invalid using Float.
//
// Parsing using the nl-NL culture:
// ' 987.654E-2' is invalid using Currency.
// ' 987.654E-2' is invalid using Float.
// ' 987,654E-2' is invalid using Currency.
// 987,654E-2 (Float) -> 9.87654
// (98765,43210) (Currency) -> -98765.43
// '(98765,43210)' is invalid using Float.
// '9,876,543.210' is invalid using Currency.
// '9,876,543.210' is invalid using Float.
// 9.876.543,210 (Currency) -> 9876543
// '9.876.543,210' is invalid using Float.
// '98_76_54_32,19' is invalid using Currency.
// '98_76_54_32,19' is invalid using Float.
//
// Parsing using the Invariant culture:
// ' 987.654E-2' is invalid using Currency.
// 987.654E-2 (Float) -> 9.87654
// ' 987,654E-2' is invalid using Currency.
// ' 987,654E-2' is invalid using Float.
// (98765,43210) (Currency) -> -9.876543E+09
// '(98765,43210)' is invalid using Float.
// 9,876,543.210 (Currency) -> 9876543
// '9,876,543.210' is invalid using Float.
// '9.876.543,210' is invalid using Currency.
// '9.876.543,210' is invalid using Float.
// 98_76_54_32,19 (Currency) -> 9.876543E+09
// '98_76_54_32,19' is invalid using Float.
Imports System.Globalization
Module Example
Public Sub Main()
' Define an array of string values.
Dim values() As String = { " 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.
Dim ci As New CultureInfo("")
ci.NumberFormat.NumberGroupSizes = New Integer() { 2 }
ci.NumberFormat.NumberGroupSeparator = "_"
' Define an array of format providers.
Dim providers() As CultureInfo = { New CultureInfo("en-US"), _
New CultureInfo("nl-NL"), ci }
' Define an array of styles.
Dim styles() As NumberStyles = { NumberStyles.Currency, NumberStyles.Float }
' Iterate the array of format providers.
For Each provider As CultureInfo In providers
Console.WriteLine("Parsing using the {0} culture:", _
If(provider.Name = String.Empty, "Invariant", provider.Name))
' Parse each element in the array of string values.
For Each value As String In values
For Each style As NumberStyles In styles
Try
Dim number As Single = Single.Parse(value, style, provider)
Console.WriteLine(" {0} ({1}) -> {2}", _
value, style, number)
Catch e As FormatException
Console.WriteLine(" '{0}' is invalid using {1}.", value, style)
Catch e As OverflowException
Console.WriteLine(" '{0}' is out of the range of a Single.", value)
End Try
Next
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' Parsing using the en-US culture:
' ' 987.654E-2' is invalid using Currency.
' 987.654E-2 (Float) -> 9.87654
' ' 987,654E-2' is invalid using Currency.
' ' 987,654E-2' is invalid using Float.
' (98765,43210) (Currency) -> -9.876543E+09
' '(98765,43210)' is invalid using Float.
' 9,876,543.210 (Currency) -> 9876543
' '9,876,543.210' is invalid using Float.
' '9.876.543,210' is invalid using Currency.
' '9.876.543,210' is invalid using Float.
' '98_76_54_32,19' is invalid using Currency.
' '98_76_54_32,19' is invalid using Float.
'
' Parsing using the nl-NL culture:
' ' 987.654E-2' is invalid using Currency.
' ' 987.654E-2' is invalid using Float.
' ' 987,654E-2' is invalid using Currency.
' 987,654E-2 (Float) -> 9.87654
' (98765,43210) (Currency) -> -98765.43
' '(98765,43210)' is invalid using Float.
' '9,876,543.210' is invalid using Currency.
' '9,876,543.210' is invalid using Float.
' 9.876.543,210 (Currency) -> 9876543
' '9.876.543,210' is invalid using Float.
' '98_76_54_32,19' is invalid using Currency.
' '98_76_54_32,19' is invalid using Float.
'
' Parsing using the Invariant culture:
' ' 987.654E-2' is invalid using Currency.
' 987.654E-2 (Float) -> 9.87654
' ' 987,654E-2' is invalid using Currency.
' ' 987,654E-2' is invalid using Float.
' (98765,43210) (Currency) -> -9.876543E+09
' '(98765,43210)' is invalid using Float.
' 9,876,543.210 (Currency) -> 9876543
' '9,876,543.210' is invalid using Float.
' '9.876.543,210' is invalid using Currency.
' '9.876.543,210' is invalid using Float.
' 98_76_54_32,19 (Currency) -> 9.876543E+09
' '98_76_54_32,19' is invalid using Float.
備註
在 .NET Core 3.0 及之後版本中,過大無法表示的數值會被四捨五入到 PositiveInfinity 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
參數 style 定義了允許在解析操作中 s 成功使用風格元素(如空白、數千個分隔符和貨幣符號)。 它必須是列舉中多個位元旗 NumberStyles 標的組合。 以下 NumberStyles 成員不獲支援:
參數 s 可包含 NumberFormatInfo.PositiveInfinitySymbol、 , NumberFormatInfo.NegativeInfinitySymbol或 NumberFormatInfo.NaNSymbol 由 指定 provider為文化的符號。 此字串比較在 .NET Core 3.0 及以後版本中不區分大小寫,但在先前版本(包括 .NET Framework)中則不區分大小寫。 根據 的 style值, s 參數也可以呈現如下形式:
[ws][$] [符號][整數數字,]整數數字[.[小數數字]][E[符號]指數數字][ws]
以方括弧 ([ 和 ]) 框架的項目是選擇性的。 下表描述每個元素。
| 元素 | 描述 |
|---|---|
| ws | 一系列空格符。 如果包含該旗幟,空白可以出現在 的s開頭,如果包含該NumberStyles.AllowTrailingWhite旗,則可以出現在 的結尾styles。NumberStyles.AllowLeadingWhitestyle |
| $ | 特定文化特性的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern當前文化的性質所定義。 當前s文化的貨幣符號可以出現style在包含NumberStyles.AllowCurrencySymbol國旗的情況下。 |
| 簽署 | 負號符號 (-) 或正負號符號 (+)。 如果包含NumberStyles.AllowLeadingSign旗幟,標誌可以出現在 的styles開頭,如果包含NumberStyles.AllowTrailingSign該旗幟,則可以出現在 的結尾sstyle。 若s包含旗標,style則可使用NumberStyles.AllowParentheses括號表示負值。 |
| 整數數字 | 一系列數位,範圍從 0 到 9,指定數位的整數部分。 如果字串包含小數位元元素,整數數字元素可以不存在。 |
| , | 特定文化特性的群組分隔符。 當前文化的群組分隔符號可以出現在 style 如果s包含旗NumberStyles.AllowThousands幟 |
| . | 特定文化特性的小數點符號。 當前文化的小數點符號可以出現sstyle在包含NumberStyles.AllowDecimalPoint國旗的情況下。 |
| 小數數字 | 一系列數位,範圍從 0 到 9,指定數位的小數部分。 若style包含NumberStyles.AllowDecimalPoint旗幟,則可出現s小數數字。 |
| E | “e” 或 “E” 字元,表示值是以指數(科學)表示法表示。 若s包含style旗標,參數NumberStyles.AllowExponent可用指數符號表示數字。 |
| 指數位數 | 指定指數的一系列數位範圍從 0 到 9。 |
注意
任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。
只有數字的字串(對應 NumberStyles.None 於樣式)若在該類型範圍內 Single ,則能成功解析。 剩餘 System.Globalization.NumberStyles 成員控制輸入字串中可能存在但不必須存在的元素。 下表說明個別 NumberStyles 旗標如何影響可能存在於 s中的元素。
| NumberStyles 值 | 除了數字外,允許加入 s 的元素 |
|---|---|
| None | 只有 整數數字 元素。 |
| AllowDecimalPoint | 小數點(.)和 小數數字 元素。 |
| AllowExponent | 表示指數表示法的 「e」 或 「E」 字元。 此旗標本身支援數字 E數字;需要額外旗標才能成功解析包含正負符號及小數點符號等元素的字串。 |
| AllowLeadingWhite | 開頭s的 w 元素。 |
| AllowTrailingWhite | 末尾的 s 元素。 |
| AllowLeadingSign | 符號 元素位於 的開頭 s。 |
| AllowTrailingSign | 符號 元素位於 的結尾 s。 |
| AllowParentheses | 符號 元素以 括號形式包含數值。 |
| AllowThousands | 千位分隔符 (,) 元素。 |
| AllowCurrencySymbol | currency ($) 元素。 |
| Currency | 所有元素。 然而,無法 s 表示十六進位數或指數符號中的數字。 |
| Float | ws 元素位於 的s開頭或結尾,符號開頭s的符號,以及小數點符號(.)。 參數 s 也可以使用指數符號。 |
| Number | 、wssign、千分符(,)和小數點(.)元素。 |
| Any | 所有元素。 然而,無法 s 代表十六進位數。 |
參數 provider 是一個 IFormatProvider 實作。 其 GetFormat 方法回傳一個 NumberFormatInfo 物件,提供關於 格式 value的文化特定資訊。 通常, provider 可以是以下任何一種:
一個 CultureInfo 代表文化的物件,提供數字格式資訊。 其 GetFormat 方法回傳 NumberFormatInfo 提供數值格式資訊的物件。
一個 NumberFormatInfo 提供格式資訊的物件。 (它的實作 GetFormat 只是回傳自己。)
一個自訂物件,實作 IFormatProvider 並使用該 GetFormat 方法實例化並提供格式資訊的 NumberFormatInfo 物件。
若provider為 ,nullNumberFormatInfo則使用 當前文化的物件。
如果 s 超出資料Single型態範圍,該方法會在 .NET Framework 及 .NET Core 2.2 及更早版本中拋出一個 。OverflowException 在 .NET Core 3.0 及更新版本中,當 小於 且 Single.PositiveInfinitySingle.MinValues 大於 Single.MaxValue時,會回傳 Single.NegativeInfinity 。s
若在解析操作中遇到參數分 s 隔符,且適用的貨幣或數字分隔符相同,則解析操作假設分隔符為十進位分隔符而非群分隔符。 欲了解更多分離子資訊,請參見 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparatorNumberGroupSeparator及 。