Single.Parse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將數字的字串表示轉換為其對等單精確度浮點數。
多載
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 和更新版本中,無法表示的值會四捨五入或 PositiveInfinityNegativeInfinity IEEE 754 規格所需的值。 在舊版中,包括 .NET Framework,剖析太大而無法表示的值會導致失敗。
Parse(String)
- 來源:
- 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 和更新版本中,無法表示的值會四捨五入或 PositiveInfinityNegativeInfinity IEEE 754 規格所需的值。 在舊版中,包括 .NET Framework,剖析太大而無法表示的值會導致失敗。
參數 s
可以包含目前文化特性的 PositiveInfinitySymbol、 NegativeInfinitySymbol、 NaNSymbol或格式的字串:
[ws][sign][integral-digits[,]]integral-digits[.[fractional-digits]][e[sign]exponential-digits][ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 一系列的空格符。 |
簽署 | 負號符號或正負號符號。 有效的符號字元取決於 NumberFormatInfo.NegativeSign 目前文化特性的 和 NumberFormatInfo.PositiveSign 屬性。 只能使用前置符號。 |
integral-digits | 一連串的數位,範圍從 0 到 9,指定數位的整數部分。 整數位數的執行可由群組分隔符分割。 例如,在某些文化特性中,逗號 (,) 分隔數千個群組。 如果字串包含小數位數元素,整數位數元素就不存在。 |
, | 特定文化特性的千位分隔符符號。 |
. | 特定文化特性的小數點符號。 |
fractional-digits | 一連串的數位,範圍從 0 到 9,指定數位的小數部分。 |
E | “e” 或 “E” 字元,表示值是以指數 (科學) 表示法表示。 |
指數數位 | 指定指數的一系列數位範圍從 0 到 9。 |
參數s
是使用 和 NumberStyles.AllowThousands 旗標的組合NumberStyles.Float來解譯。 這表示允許空格符和千位分隔符,但貨幣符號則不允許。 若要明確定義元素 (,例如貨幣符號、千位分隔符和空格符,) 可以存在於 中 s
,請使用 Parse(String, NumberStyles) 方法多載。
參數 s
是使用物件中 NumberFormatInfo 針對目前系統文化特性初始化的格式信息進行剖析。 如需詳細資訊,請參閱CurrentInfo。 若要使用特定文化特性的格式資訊剖析字串,請使用 Parse(String, IFormatProvider) 或 Parse(String, NumberStyles, IFormatProvider) 方法。
一般而言,如果您傳遞 Parse 方法所建立 ToString 的字串,則會傳回原始 Single 值。 不過,由於精確度遺失,值可能不相等。
如果 s
超出資料類型的範圍Single,方法會在 .NET Framework 和 .NET Core 2.2 和舊版擲回 OverflowException 。 在 .NET Core 3.0 和更新版本上,如果 s
小於 Single.MinValue 且 s
Single.PositiveInfinity 大於 Single.MaxValue,則會傳回 Single.NegativeInfinity 。
如果在剖析作業期間於 參數中 s
遇到分隔符,且適用的貨幣或數位十進位和群組分隔符相同,則剖析作業會假設分隔符是小數分隔符,而不是群組分隔符。 如需分隔符的詳細資訊,請參閱CurrencyDecimalSeparator、 NumberDecimalSeparatorCurrencyGroupSeparator、 和 NumberGroupSeparator。
另請參閱
適用於
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- 來源:
- 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
將字元範圍剖析為值。
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
將數字的字串表示 (使用指定樣式) 轉換為其對等單精確度浮點數。
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 和更新版本中,無法表示的值會四捨五入或 PositiveInfinityNegativeInfinity IEEE 754 規格所需的值。 在舊版中,包括 .NET Framework,剖析太大而無法表示的值會導致失敗。
參數 style
會定義樣式專案 (,例如空格符、千位分隔符和貨幣符號,) 參數中 s
允許的樣式專案,讓剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 不支援下列 NumberStyles 成員:
參數s
可以包含目前文化特性的PositiveInfinitySymbol、 NegativeInfinitySymbolNaNSymbol。 視 的值 style
而定,它也可以採用下列格式:
[ws][$][sign][integral-digits[,]]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
Ws 一系列的空格符。 如果style
包含 旗標,NumberStyles.AllowLeadingWhite則空格符可以出現在的s
開頭,如果style
包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 結尾s
。
$ 特定文化特性的貨幣符號。 字串中的位置是由目前文化特性的 和 NumberFormatInfo.CurrencyPositivePattern 屬性所NumberFormatInfo.CurrencyNegativePattern定義。 如果style
包含 NumberStyles.AllowCurrencySymbol 旗標,則目前文化特性的貨幣符號可以出現在 s
中。
標誌 負號符號 ( ) 或正負號符號 (+) 。 如果 style
包含旗標,NumberStyles.AllowLeadingSign則符號可以出現在的s
開頭,如果style
包含 NumberStyles.AllowTrailingSign 旗標,則它可能會出現在 結尾s
。 如果style
包含 NumberStyles.AllowParentheses 旗標,可以使用 s
括號來表示負值。
integral-digits 一連串的數位,範圍從 0 到 9,指定數位的整數部分。 如果字串包含小數位數元素,整數位數元素就不存在。
,特定文化特性的群組分隔符。 如果style
包含 NumberStyles.AllowThousands 旗標,則目前文化特性的群組分隔符符號可以出現在 中s
.
特定文化特性的小數點符號。 如果style
包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可以出現在 中s
。
fractional-digits 一連串的數位,範圍從 0 到 9,指定數位的小數部分。 如果style
包含旗標,NumberStyles.AllowDecimalPoint則小數位數可以出現在 中s
。
E “e” 或 “E” 字元,表示值是以指數 (科學) 表示法表示。 如果style
包含旗標,參數value
可以表示指數表示法的數位NumberStyles.AllowExponent。
指數數位 指定指數的一系列數位範圍從 0 到 9。
注意
不論自變數的值style
為何,剖析作業都會忽略 中s
任何終止的 NUL (U+0000) 字元。
只有數位的字串 (對應至 NumberStyles.None 樣式) 在類型範圍內 Single 時一律會成功剖析。 可能存在但不需要存在於輸入字串中的其餘 System.Globalization.NumberStyles 成員控件元素。 下表指出個別 NumberStyles 旗標如何影響 中 s
可能存在的專案。
NumberStyles 值 | 除了數位之外允許 s 的專案 |
---|---|
None | 僅限 整數數字元素 。 |
AllowDecimalPoint | 小數點 (。) 和 小數位數 元素。 |
AllowExponent | 表示指數表示法的 「e」 或 「E」 字元。 此旗標本身 支援格式為E位數的值;需要額外的旗標,才能成功剖析具有正數或負號和小數點符號等元素的字串。 |
AllowLeadingWhite | 開頭的 s ws 元素。 |
AllowTrailingWhite | 結尾處的 s ws 元素。 |
AllowLeadingSign | 開頭的s 符號專案。 |
AllowTrailingSign | 結尾處的s 符號專案。 |
AllowParentheses | 以括弧括住數值形式的 sign 元素。 |
AllowThousands | 千位分隔符 (,) 元素。 |
AllowCurrencySymbol | 貨幣 ($) 元素。 |
Currency | 所有元素。 不過, s 不能代表十六進位數或指數表示法中的數位。 |
Float | 開頭或結尾的 s ws 元素,會在 開頭加上s 小數點 (.) 符號。 參數 s 也可以使用指數表示法。 |
Number |
ws 、sign 、thousands 分隔符 (、) 和小數點 (.) 元素。 |
Any | 所有元素。 不過, s 不能代表十六進位數。 |
的一些範例 s
包括 “100”、“-123、456、789”、“123.45e+6”、“+500”、“5e2”、“3.1416”、“600.”、“-.123”和 “-Infinity”。
參數 s
會使用針對目前系統文化特性初始化的 物件中的 NumberFormatInfo 格式化資訊進行剖析。 若要指定用於剖析作業的格式資訊的文化特性,請呼叫 Parse(String, NumberStyles, IFormatProvider) 多載。
一般而言,如果您傳遞 Parse 方法所建立 ToString 的字串,則會傳回原始 Single 值。 不過,由於精確度遺失,值可能不相等。
如果 s
超出資料類型的範圍Single,方法會在 .NET Framework 和 .NET Core 2.2 和舊版上擲回 OverflowException 。 在 .NET Core 3.0 和更新版本上,如果 s
小於 Single.MinValue 且 s
Single.PositiveInfinity 大於 Single.MaxValue,則會傳回 Single.NegativeInfinity 。
如果在剖析作業期間於 參數中 s
遇到分隔符,且適用的貨幣或數位十進位和群組分隔符相同,則剖析作業會假設分隔符是小數分隔符,而不是群組分隔符。 如需分隔符的詳細資訊,請參閱CurrencyDecimalSeparator、 NumberDecimalSeparatorCurrencyGroupSeparator、 和 NumberGroupSeparator。
另請參閱
適用於
Parse(String, IFormatProvider)
- 來源:
- 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 窗體的按鈕 click 事件處理程式。 它會使用 屬性傳回的 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 和更新版本中,無法表示的值會四捨五入或 PositiveInfinityNegativeInfinity IEEE 754 規格所需的值。 在舊版中,包括 .NET Framework,剖析太大而無法表示的值會導致失敗。
這個多載通常用來將各種格式 Single 的文字轉換成值。 例如,它可以用來將使用者輸入的文字轉換成 HTML 文字框,轉換為數值。
參數s
是使用 和 NumberStyles.AllowThousands 旗標的組合NumberStyles.Float來解譯。 參數 s
可以包含 NumberFormatInfo.PositiveInfinitySymbol、 NumberFormatInfo.NegativeInfinitySymbol或 NumberFormatInfo.NaNSymbol 指定的文化 provider
特性,也可以包含格式的字串:
[ws][sign]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws]
選擇性元素會以方括弧括住, ([ 和 ]) 。 包含「數位」一詞的專案包含一系列範圍從 0 到 9 的數位字元。
元素 | 描述 |
---|---|
ws | 一系列的空格符。 |
簽署 | 負號符號 ( ) 或正負號符號 (+) 。 |
integral-digits | 一連串的數位,範圍從 0 到 9,指定數位的整數部分。 整數位數的執行可由群組分隔符分割。 例如,在某些文化特性中,逗號 (,) 分隔數千個群組。 如果字串包含小數位數元素,整數位數元素就不存在。 |
. | 特定文化特性的小數點符號。 |
fractional-digits | 一連串的數位,範圍從 0 到 9,指定數位的小數部分。 |
E | “e” 或 “E” 字元,表示值是以指數 (科學) 表示法表示。 |
指數數位 | 指定指數的一系列數位範圍從 0 到 9。 |
如需數值格式的詳細資訊,請參閱 格式化類型 主題。
參數 provider
是實作 IFormatProvider ,其 GetFormat 方法會 NumberFormatInfo 傳回提供特定文化特性格式信息的物件。
Parse(String, IFormatProvider)叫用 方法時,它會呼叫 provider
參數的 方法,並傳遞Type代表NumberFormatInfo型別的 GetFormat 物件。
GetFormat然後,方法會NumberFormatInfo傳回 物件,提供參數格式s
的相關信息。 有三種方式可以使用 provider
參數,將自定義格式資訊提供給剖析作業:
您可以傳遞 CultureInfo 物件,此物件代表提供格式化資訊的文化特性。 其 GetFormat 方法會傳 NumberFormatInfo 回 物件,提供該文化特性的數值格式資訊。
您可以傳遞提供數值格式資訊的實際 NumberFormatInfo 物件。 (其 實作 GetFormat 只會傳回本身。)
您可以傳遞實作 的 IFormatProvider自訂物件。 其 GetFormat 方法會具現化並傳回 NumberFormatInfo 提供格式信息的物件。
如果 provider
為 null
或 NumberFormatInfo 無法取得 ,則會使用目前系統文化特性的格式資訊。
如果 s
超出資料類型的範圍Single,方法會在 .NET Framework 和 .NET Core 2.2 和舊版上擲回 OverflowException 。 在 .NET Core 3.0 和更新版本上,如果 s
小於 Single.MinValue 且 s
Single.PositiveInfinity 大於 Single.MaxValue,則會傳回 Single.NegativeInfinity 。
如果在剖析作業期間於 參數中 s
遇到分隔符,且適用的貨幣或數位十進位和群組分隔符相同,則剖析作業會假設分隔符是小數分隔符,而不是群組分隔符。 如需分隔符的詳細資訊,請參閱CurrencyDecimalSeparator、 NumberDecimalSeparatorCurrencyGroupSeparator和 NumberGroupSeparator。
的 s
一些範例包括 “100”、“-123、456、789”、“123.45e+6”、“+500”、“5e2”、“3.1416”、“600.”、“-.123” 和 “-Infinity”。
另請參閱
適用於
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- 來源:
- 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
將包含數字字串表示 (使用指定樣式和特定文化特性格式) 的字元範圍轉換為其對等單精確度浮點數。
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 IEEE 754 規格所需的或 NegativeInfinity 。 在舊版中,包括 .NET Framework,剖析太大而無法表示的值會導致失敗。
如果 s
超出數據類型的範圍Single,則如果 s
小於 Single.MinValue 且 Single.PositiveInfinitys
Single.MaxValue大於 ,則方法會傳回 。Single.NegativeInfinity
適用於
Parse(String, NumberStyles, IFormatProvider)
- 來源:
- 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 IEEE 754 規格所需的或 NegativeInfinity 。 在舊版中,包括 .NET Framework,剖析太大而無法表示的值會導致失敗。
參數 style
會定義樣式專案 (,例如空格符、千位分隔符,以及參數中 s
允許的貨幣符號) ,讓剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 不支援下列 NumberStyles 成員:
參數 s
可以包含 NumberFormatInfo.PositiveInfinitySymbol、 NumberFormatInfo.NegativeInfinitySymbol或 NumberFormatInfo.NaNSymbol ,供 所指定的文化特性使用 provider
。 視 的值 style
而定,它也可以採用下列格式:
[ws][] [$sign][integral-digits,]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws]
以方括弧 ([ 和 ]) 框架的項目是選擇性的。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 一系列空格符。 如果 包含 旗標,則空格元可以出現在 的s 開頭,如果包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 結尾style s 。NumberStyles.AllowLeadingWhitestyle |
$ | 特定文化特性的貨幣符號。 字串中的位置是由 NumberFormatInfo.CurrencyNegativePattern 目前文化特性的 和 NumberFormatInfo.CurrencyPositivePattern 屬性所定義。 如果style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前文化特性的貨幣符號可能會出現在 中s 。 |
簽署 | 負號符號 ( ) 或正負號符號 (+) 。 如果 style 包含旗標,則符號可以出現在的s 開頭,如果包含 NumberStyles.AllowTrailingSign 旗標,則會出現在 的style s NumberStyles.AllowLeadingSign結尾。 如果包含 NumberStyles.AllowParentheses 旗標,則可以在 中使用s 括弧來表示負值style 。 |
整數數位 | 一系列的數字,範圍從0到9,指定數位的整數部分。 如果字串包含fractional-digits元素,整數數字元素就可能不存在。 |
, | 特定文化特性的群組分隔符。 如果style 包含 NumberStyles.AllowThousands 旗標,則目前文化特性的群組分隔符符號可能會出現在 中s |
. | 特定文化特性的小數點符號。 如果style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前的文化特性小數點符號可能會出現在 中s 。 |
fractional-digits | 一系列的數字,範圍從0到9,指定數位的小數部分。 如果style 包含旗標,NumberStyles.AllowDecimalPoint則可以在 中s 顯示小數位數。 |
E | “e” 或 “E” 字元,表示該值是以指數 (科學) 表示法表示。 如果style 包含 NumberStyles.AllowExponent 旗標,參數s 可以代表指數表示法的數位。 |
指數數位 | 一系列數位,範圍從 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 ws 元素。 |
AllowTrailingWhite | 結尾處的 s ws 專案。 |
AllowLeadingSign | 開頭的s 符號專案。 |
AllowTrailingSign | 結尾的s 符號專案。 |
AllowParentheses | 以括弧括住數值形式的 sign 元素。 |
AllowThousands | 千位分隔符 (,) 元素。 |
AllowCurrencySymbol | 貨幣 ($) 專案。 |
Currency | 所有元素。 不過, s 不能以指數表示法表示十六進位數位或數位。 |
Float | 開頭或結尾的 s ws 元素,在 開頭s 為 符號,而小數點 (.) 符號。 參數 s 也可以使用指數表示法。 |
Number |
ws 、、sign thousands 分隔符 (、) 和小數點 (.) 元素。 |
Any | 所有元素。 不過, s 不能代表十六進位數位。 |
參數 provider
是實作 IFormatProvider 。 其 GetFormat 方法會 NumberFormatInfo 傳回 物件,這個物件提供 有關 格式 value
的文化特性特定資訊。 一般而言, provider
可以是下列任一項:
CultureInfo物件,表示提供數值格式資訊的文化特性。 其 GetFormat 方法會 NumberFormatInfo 傳回提供數值格式信息的物件。
提供 NumberFormatInfo 格式化信息的物件。 (其 實作 GetFormat 只會傳回本身。)
實作 IFormatProvider 和使用 方法的 GetFormat 自定義物件,可具現化及傳回 NumberFormatInfo 提供格式化信息的物件。
如果 provider
為 null
,則會 NumberFormatInfo 使用目前文化特性的物件。
如果 s
超出資料類型的範圍Single,方法會在 .NET Framework 和 .NET Core 2.2 和舊版上擲回 OverflowException 。 在 .NET Core 3.0 和更新版本上,如果 s
小於 Single.MinValue 且 s
Single.PositiveInfinity 大於 Single.MaxValue,則會傳回 Single.NegativeInfinity 。
如果在剖析作業期間於 參數中 s
遇到分隔符,且適用的貨幣或數位十進位和群組分隔符相同,則剖析作業會假設分隔符是小數分隔符,而不是群組分隔符。 如需分隔符的詳細資訊,請參閱CurrencyDecimalSeparator、 NumberDecimalSeparatorCurrencyGroupSeparator、 和 NumberGroupSeparator。
另請參閱
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應