Single.TryParse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將數位的字串表示轉換為其對等的單精度浮點數。 傳回值表示轉換成功或失敗。
多載
| 名稱 | Description |
|---|---|
| TryParse(String, IFormatProvider, Single) |
嘗試將字串剖析成值。 |
| TryParse(ReadOnlySpan<Char>, Single) |
將字元範圍中數位的字串表示轉換為其對等的單精度浮點數。 傳回值表示轉換成功或失敗。 |
| TryParse(String, Single) |
將數位的字串表示轉換為其對等的單精度浮點數。 傳回值表示轉換成功或失敗。 |
| TryParse(ReadOnlySpan<Byte>, IFormatProvider, Single) |
嘗試將UTF-8字元的範圍剖析為值。 |
| TryParse(ReadOnlySpan<Char>, IFormatProvider, Single) |
嘗試將字元範圍剖析成值。 |
| TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Single) |
嘗試將UTF-8字元的範圍剖析為值。 |
| TryParse(ReadOnlySpan<Byte>, Single) |
嘗試將包含數位字串表示的UTF-8字元範圍轉換為其單精度浮點數對等專案。 |
| TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single) |
將指定樣式和特定文化特性格式的數位範圍表示轉換為其單精度浮點數對等專案。 傳回值表示轉換成功或失敗。 |
| TryParse(String, NumberStyles, IFormatProvider, Single) |
將指定樣式和特定文化特性格式之數位的字串表示轉換為其單精度浮點數對等專案。 傳回值表示轉換成功或失敗。 |
備註
在 .NET Core 3.0 及之後版本中,過大無法表示的數值會被四捨五入到 PositiveInfinity 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
TryParse(String, IFormatProvider, Single)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
嘗試將字串剖析成值。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result) = IParsable<float>::TryParse;
public static bool TryParse(string? s, IFormatProvider? provider, out float result);
static member TryParse : string * IFormatProvider * single -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Single) As Boolean
參數
- s
- String
要剖析的字串。
- provider
- IFormatProvider
一個提供關於 s的文化特定格式資訊的物件。
- result
- Single
當此方法回傳時,包含成功解析 s 的結果,或失敗時為未定義的值。
傳回
true 若 s 成功解析;否則, false。
適用於
TryParse(ReadOnlySpan<Char>, Single)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
將字元範圍中數位的字串表示轉換為其對等的單精度浮點數。 傳回值表示轉換成功或失敗。
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] float % result);
public static bool TryParse(ReadOnlySpan<char> s, out float result);
static member TryParse : ReadOnlySpan<char> * single -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Single) As Boolean
參數
- s
- ReadOnlySpan<Char>
>字元範圍包含要轉換數字的字串表示。
- result
- Single
當此方法回傳時,若轉換成功,則包含參數的單精度浮點數等價 s 值;若轉換失敗則為零。 若 s 參數為 null 空或非有效格式中的數字,則轉換失敗。 若 s 是小於 Single.MinValue 的有效數,則 result 。NegativeInfinity 若 s 是大於 Single.MaxValue 的有效數值,result則 。PositiveInfinity 此參數以未初始化的方式傳遞;原本輸入 result 的任何值都會被覆寫。
傳回
true若s成功轉換;否則,。 false
備註
在 .NET Core 3.0 及之後版本中,過大無法表示的數值會被四捨五入到 PositiveInfinity 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
適用於
TryParse(String, Single)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
將數位的字串表示轉換為其對等的單精度浮點數。 傳回值表示轉換成功或失敗。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] float % result);
public static bool TryParse(string s, out float result);
public static bool TryParse(string? s, out float result);
static member TryParse : string * single -> bool
Public Shared Function TryParse (s As String, ByRef result As Single) As Boolean
參數
- s
- String
字串,表示要轉換的數位。
- result
- Single
當此方法回傳時,包含與 中 s數值或符號等同的單精度浮點數(若轉換成功);若轉換失敗則為零。 若s參數在有效格式中是nullEmpty或不是數字,轉換即失敗。 在 .NET Framework 上,若 s 代表的數字小於 Single.MinValue 或大於 Single.MaxValue 也會失敗。 此參數以未初始化的方式傳遞;原本輸入 result 的任何值都會被覆寫。
傳回
true若s成功轉換;否則,。 false
範例
以下範例使用此 TryParse(String, Single) 方法將數 Single 值的字串表示轉換為數值。 它假設 en-US 是當前的文化。
string value;
float number;
// Parse a floating-point value with a thousands separator.
value = "1,643.57";
if (Single.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// Parse a floating-point value with a currency symbol and a
// thousands separator.
value = "$1,643.57";
if (Single.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// Parse value in exponential notation.
value = "-1.643e6";
if (Single.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// Parse a negative integer value.
value = "-168934617882109132";
if (Single.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// The example displays the following output:
// 1643.57
// Unable to parse '$1,643.57'.
// -164300
// -1.689346E+17
// Parse a floating-point value with a thousands separator.
let value = "1,643.57"
match Single.TryParse value with
| true, number ->
printfn $"{number}"
| _ ->
printfn $"Unable to parse '{value}'."
// Parse a floating-point value with a currency symbol and a
// thousands separator.
let value = "$1,643.57"
match Single.TryParse value with
| true, number ->
printfn $"{number}"
| _ ->
printfn $"Unable to parse '{value}'."
// Parse value in exponential notation.
let value = "-1.643e6"
match Single.TryParse value with
| true, number ->
printfn $"{number}"
| _ ->
printfn $"Unable to parse '{value}'."
// Parse a negative integer value.
let value = "-168934617882109132"
match Single.TryParse value with
| true, number ->
printfn $"{number}"
| _ ->
printfn $"Unable to parse '{value}'."
// The example displays the following output:
// 1643.57
// Unable to parse '$1,643.57'.
// -164300
// -1.689346E+17
Dim value As String
Dim number As Single
' Parse a floating-point value with a thousands separator.
value = "1,643.57"
If Single.TryParse(value, number) Then
Console.WriteLine(number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
' Parse a floating-point value with a currency symbol and a
' thousands separator.
value = "$1,643.57"
If Single.TryParse(value, number) Then
Console.WriteLine(number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
' Parse value in exponential notation.
value = "-1.643e6"
If Single.TryParse(value, number)
Console.WriteLine(number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
' Parse a negative integer number.
value = "-168934617882109132"
If Single.TryParse(value, number)
Console.WriteLine(number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
' The example displays the following output:
' 1643.57
' Unable to parse '$1,643.57'.
' -1643000
' -1.689346E+17
備註
在 .NET Core 3.0 及之後版本中,過大無法表示的數值會被四捨五入到 PositiveInfinity 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
此過載與方法 Single.Parse(String) 不同,回傳一個布林值以指示解析操作是否成功,而非回傳解析後的數值。 它消除了在無法成功解析的 event FormatException 時,使用例外處理來測試 a s 的必要。
參數可以 s 包含 PositiveInfinitySymbol、 NegativeInfinitySymbol或 NaNSymbol 符號。 此字串比較在 .NET Core 3.0 及以後版本中不區分大小寫,但在先前版本(包括 .NET Framework)中則不區分大小寫。 參數 s 也可以是如下形式的字串:
[ws][sign][integral-digits,]integral-digits[.[fractional-digits][e[sign]exponential-digits][ws]
方括弧中的元素是選擇性的。 下表描述每個元素。
| 元素 | 描述 |
|---|---|
| ws | 一系列空格符。 |
| 簽署 | 負號或正負號符號。 |
| 整數數字 | 一系列數值字元,範圍從 0 到 9,指定數位的整數部分。 如果有小數位數,整數位數就可能不存在。 |
| , | 特定文化特性的群組分隔符符號。 |
| . | 特定文化特性的小數點符號。 |
| 小數數字 | 一系列數值字元,範圍從 0 到 9,指定數位的小數部分。 |
| E | 表示指數型(科學)表示法的大寫或小寫字元 『e』。 |
| 指數位數 | 一系列數值字元,範圍從 0 到 9,指定指數。 |
s參數是透過 和 NumberStyles.Float 旗標的組合NumberStyles.AllowThousands來解釋的。 這表示允許空格符和千位分隔符,但貨幣符號則不允許。 要明確定義 中可能存在 s的元素(如貨幣符號、數千個分隔符和空白),可以使用 TryParse(String, NumberStyles, IFormatProvider, Single) 方法過載。
s參數是利用為當前系統文化初始化的物件格式資訊NumberFormatInfo進行解析。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo。 若要解析字串,使用其他指定文化的格式資訊,請使用 TryParse(String, NumberStyles, IFormatProvider, Single) 方法超載。
通常,如果你傳 Single.TryParse 給方法一個由呼叫 Single.ToString 該方法所產生的字串, Single 原始值就會被回傳。 不過,由於精確度遺失,值可能不相等。
若 s 超出資料型別範圍 Single ,該方法會在 .NET Framework 上回傳 false 。 在 .NET Core 3.0 及更新版本中,當 小於 且 Single.NegativeInfinitysSingle.MinValue 大於 Single.PositiveInfinity時,會回傳 s 。Single.MaxValue
若在解析操作中遇到參數分 s 隔符,且適用的貨幣或數字分隔符相同,則解析操作假設分隔符為十進位分隔符而非群分隔符。 欲了解更多分離子資訊,請參見 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparatorNumberGroupSeparator及 。
另請參閱
適用於
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Single)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
嘗試將UTF-8字元的範圍剖析為值。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result) = IUtf8SpanParsable<float>::TryParse;
public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out float result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * single -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As Single) As Boolean
參數
- utf8Text
- ReadOnlySpan<Byte>
要剖析的UTF-8字元範圍。
- provider
- IFormatProvider
一個提供關於 utf8Text的文化特定格式資訊的物件。
- result
- Single
回傳時,包含成功解析 utf8Text 的結果,失敗時則為未定義的值。
傳回
true 若 utf8Text 成功解析;否則, false。
適用於
TryParse(ReadOnlySpan<Char>, IFormatProvider, Single)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
嘗試將字元範圍剖析成值。
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result) = ISpanParsable<float>::TryParse;
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out float result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * single -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Single) As Boolean
參數
- s
- ReadOnlySpan<Char>
要剖析的字元範圍。
- provider
- IFormatProvider
一個提供關於 s的文化特定格式資訊的物件。
- result
- Single
當此方法回傳時,包含成功解析 s的結果,或失敗時為未定義的值。
傳回
true 若 s 成功解析;否則, false。
適用於
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Single)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
嘗試將UTF-8字元的範圍剖析為值。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result) = System::Numerics::INumberBase<float>::TryParse;
public static bool TryParse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out float result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * single -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As Single) As Boolean
參數
- utf8Text
- ReadOnlySpan<Byte>
要剖析的UTF-8字元範圍。
- style
- NumberStyles
一個可以存在 utf8Text於 的數字樣式的位元組合。
- provider
- IFormatProvider
一個提供關於 utf8Text的文化特定格式資訊的物件。
- result
- Single
回傳時,包含成功解析 utf8Text 的結果,失敗時則為未定義的值。
傳回
true 若 utf8Text 成功解析;否則, false。
適用於
TryParse(ReadOnlySpan<Byte>, Single)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
嘗試將包含數位字串表示的UTF-8字元範圍轉換為其單精度浮點數對等專案。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] float % result);
public static bool TryParse(ReadOnlySpan<byte> utf8Text, out float result);
static member TryParse : ReadOnlySpan<byte> * single -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Single) As Boolean
參數
- utf8Text
- ReadOnlySpan<Byte>
唯讀 UTF-8 字元範圍,其中包含要轉換的數位。
- result
- Single
當此方法回傳時,包含一個單精度浮點數,等同於若轉換成功時所 utf8Text 包含的數值或符號,若轉換失敗則為零。 如果格式utf8TextEmpty有效或不正確,轉換即失敗。 這個參數會未初始化傳遞;將覆寫原本在結果中提供的任何值。
傳回
true若utf8Text成功轉換;否則,。 false
適用於
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
將指定樣式和特定文化特性格式的數位範圍表示轉換為其單精度浮點數對等專案。 傳回值表示轉換成功或失敗。
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result) = System::Numerics::INumberBase<float>::TryParse;
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result);
public static bool TryParse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out float result);
public static bool TryParse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out float result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * single -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Single) As Boolean
參數
- s
- ReadOnlySpan<Char>
唯讀字元範圍,其中包含要轉換的數位。 張成是用 所 style指定的風格來解釋的。
- style
- NumberStyles
一個位元組合的列舉值,表示允許 s的格式。 典型的指定值是 Float 與 AllowThousands結合。
- provider
- IFormatProvider
一個提供關於 s的文化特定格式資訊的物件。
- result
- Single
當此方法回傳時,包含與中 s數值或符號等價的單精度浮點數(若轉換成功);若轉換失敗則為零。 若s參數為nullEmpty或 ,格式不符合style,則轉換失敗,該數字小於 Single.MinValue 或大於 Single.MaxValue,或 style 不是有效列舉常數組合NumberStyles。 此參數以未初始化的方式傳遞;原本輸入 result 的任何值都會被覆寫。
傳回
true若s成功轉換;否則,。 false
備註
在 .NET Core 3.0 及之後版本中,過大無法表示的數值會被四捨五入到 PositiveInfinity 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
適用於
TryParse(String, NumberStyles, IFormatProvider, Single)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
將指定樣式和特定文化特性格式之數位的字串表示轉換為其單精度浮點數對等專案。 傳回值表示轉換成功或失敗。
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result);
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result) = System::Numerics::INumberBase<float>::TryParse;
public static bool TryParse(string s, System.Globalization.NumberStyles style, IFormatProvider provider, out float result);
public static bool TryParse(string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out float result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * single -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Single) As Boolean
參數
- s
- String
字串,表示要轉換的數位。
- style
- NumberStyles
一個位元組合的列舉值,表示允許 s的格式。 典型的指定值是 Float 與 AllowThousands結合。
- provider
- IFormatProvider
一個提供關於 s的文化特定格式資訊的物件。
- result
- Single
當此方法回傳時,包含與中 s數值或符號等價的單精度浮點數(若轉換成功);若轉換失敗則為零。 若s參數為Emptynull或,格式不符合style,或style非列舉常數的有效組合NumberStyles,則轉換失敗。 在 .NET Framework 上,若 s 代表的數字小於 Single.MinValue 或大於 Single.MaxValue 也會失敗。 此參數以未初始化的方式傳遞;原本輸入 result 的任何值都會被覆寫。
傳回
true若s成功轉換;否則,。 false
例外狀況
範例
以下範例展示了如何運用此 Single.TryParse(String, NumberStyles, IFormatProvider, Single) 方法解析具有特定風格且格式化特定文化慣例的數字字串表示。
string value;
System.Globalization.NumberStyles style;
System.Globalization.CultureInfo culture;
float number;
// Parse currency value using en-GB culture.
value = "£1,097.63";
style = System.Globalization.NumberStyles.Number |
System.Globalization.NumberStyles.AllowCurrencySymbol;
culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
if (Single.TryParse(value, style, culture, out number))
Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
Console.WriteLine("Unable to convert '{0}'.", value);
value = "1345,978";
style = System.Globalization.NumberStyles.AllowDecimalPoint;
culture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR");
if (Single.TryParse(value, style, culture, out number))
Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
Console.WriteLine("Unable to convert '{0}'.", value);
value = "1.345,978";
style = System.Globalization.NumberStyles.AllowDecimalPoint |
System.Globalization.NumberStyles.AllowThousands;
culture = System.Globalization.CultureInfo.CreateSpecificCulture("es-ES");
if (Single.TryParse(value, style, culture, out number))
Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
Console.WriteLine("Unable to convert '{0}'.", value);
value = "1 345,978";
if (Single.TryParse(value, style, culture, out number))
Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
Console.WriteLine("Unable to convert '{0}'.", value);
// The example displays the following output:
// Converted '£1,097.63' to 1097.63.
// Converted '1345,978' to 1345.978.
// Converted '1.345,978' to 1345.978.
// Unable to convert '1 345,978'.
// Parse currency value using en-GB culture.
let value = "£1,097.63"
let style = System.Globalization.NumberStyles.Number ||| System.Globalization.NumberStyles.AllowCurrencySymbol
let culture = System.Globalization.CultureInfo.CreateSpecificCulture "en-GB"
match Single.TryParse(value, style, culture) with
| true, number ->
printfn $"Converted '{value}' to {number}."
| _ ->
printfn $"Unable to convert '{value}'."
let value = "1345,978"
let style = System.Globalization.NumberStyles.AllowDecimalPoint
let culture = System.Globalization.CultureInfo.CreateSpecificCulture "fr-FR"
match Single.TryParse(value, style, culture) with
| true, number ->
printfn $"Converted '{value}' to {number}."
| _ ->
printfn $"Unable to convert '{value}'."
let value = "1.345,978"
let style = System.Globalization.NumberStyles.AllowDecimalPoint ||| System.Globalization.NumberStyles.AllowThousands
let culture = System.Globalization.CultureInfo.CreateSpecificCulture "es-ES"
match Single.TryParse(value, style, culture) with
| true, number ->
printfn $"Converted '{value}' to {number}."
| _ ->
printfn $"Unable to convert '{value}'."
let value = "1 345,978"
match Single.TryParse(value, style, culture) with
| true, number ->
printfn $"Converted '{value}' to {number}."
| _ ->
printfn $"Unable to convert '{value}'."
// The example displays the following output:
// Converted '£1,097.63' to 1097.63.
// Converted '1345,978' to 1345.978.
// Converted '1.345,978' to 1345.978.
// Unable to convert '1 345,978'.
Dim value As String
Dim style As System.Globalization.NumberStyles
Dim culture As System.Globalization.CultureInfo
Dim number As Single
' Parse currency value using en-GB culture.
value = "£1,097.63"
style = System.Globalization.NumberStyles.Number Or _
System.Globalization.NumberStyles.AllowCurrencySymbol
culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB")
If Single.TryParse(value, style, culture, number) Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
Console.WriteLine("Unable to convert '{0}'.", value)
End If
value = "1345,978"
style = System.Globalization.NumberStyles.AllowDecimalPoint
culture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR")
If Single.TryParse(value, style, culture, number) Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
Console.WriteLine("Unable to convert '{0}'.", value)
End If
value = "1.345,978"
style = System.Globalization.NumberStyles.AllowDecimalPoint Or _
System.Globalization.NumberStyles.AllowThousands
culture = System.Globalization.CultureInfo.CreateSpecificCulture("es-ES")
If Single.TryParse(value, style, culture, number) Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
Console.WriteLine("Unable to convert '{0}'.", value)
End If
value = "1 345,978"
If Single.TryParse(value, style, culture, number) Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
Console.WriteLine("Unable to convert '{0}'.", value)
End If
' The example displays the following output:
' Converted '£1,097.63' to 1097.63.
' Converted '1345,978' to 1345.978.
' Converted '1.345,978' to 1345.978.
' Unable to convert '1 345,978'.
備註
在 .NET Core 3.0 及之後版本中,過大無法表示的數值會被四捨五入到 PositiveInfinity 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
此過載與方法 Parse(String, NumberStyles, IFormatProvider) 不同,回傳一個布林值以指示解析操作是否成功,而非回傳解析後的數值。 它消除了在無法成功解析的 event FormatException 時,使用例外處理來測試 a s 的必要。
參數 style 定義了該參數允許的格式 s ,以確保解析操作成功。 它必須是列舉中多個位元旗 NumberStyles 標的組合。 以下 NumberStyles 成員不獲支援:
s參數可包含 PositiveInfinitySymbol、 NegativeInfinitySymbol,或NaNSymbol由 表示provider的文化符號。 此字串比較在 .NET Core 3.0 及以後版本中不區分大小寫,但在先前版本(包括 .NET Framework)中則不區分大小寫。 此外,根據 的styles值,參數可能包含以下元素:
[ws][$][sign][integral-digits,]integral-digits[.fractional-digits][e[sign]exponential-digits][ws]
方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。
| 元素 | 描述 |
|---|---|
| ws | 選擇性的空格符。 如果包含NumberStyles.AllowLeadingWhite旗幟,sstyle開頭可能會出現空白。 它可能出現在包含旗幟的最後sstyle。NumberStyles.AllowTrailingWhite |
| $ | 特定文化特性的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyNegativePattern參數方法provider回傳的IFormatProvider.GetFormat物件的 or NumberFormatInfo.CurrencyPositivePattern 屬性NumberFormatInfo所定義。 貨幣s符號可出現style在包含NumberStyles.AllowCurrencySymbol旗幟時。 |
| 簽署 | 選擇性符號。 如果包含s旗幟,標誌可以出現在 的styleNumberStyles.AllowLeadingSign開頭,如果包含s該旗幟,則可以出現在 的結尾styleNumberStyles.AllowTrailingSign。 若s包含旗標,style則可使用NumberStyles.AllowParentheses括號表示負值。 |
| 整數數字 | 一系列數位,範圍從 0 到 9,指定數位的整數部分。 如果有小數位數,整數位數就可能不存在。 |
| , | 特定文化特性的千位分隔符符號。 當前s文化的千分符號若包含style國旗,則可出現NumberStyles.AllowThousands。 |
| . | 特定文化特性的小數點符號。 當前文化的小數點符號可以出現sstyle在包含NumberStyles.AllowDecimalPoint國旗的情況下。 |
| 小數數字 | 一系列數位,範圍從 0 到 9,指定數位的小數部分。 若s包含style旗幟,則可出現NumberStyles.AllowDecimalPoint小數數字。 |
| e | e 或 E 字元表示 , s 可以用指數符號表示一個數字。 如果風格包含旗幟,NumberStyles.AllowExponent參數s可以用指數符號表示數字。 |
| 指數位數 | 指定指數的一系列數位範圍從 0 到 9。 |
注意
任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。
只有數字的字串(對應 NumberStyles.None 於樣式)若在該類型範圍內 Single ,則能成功解析。 剩餘 System.Globalization.NumberStyles 成員控制輸入字串中可能存在但不必須存在的元素。 下表說明個別 NumberStyles 旗標如何影響可能存在於 s中的元素。
| NumberStyles 值 | 除了數位以外,允許的 元素 |
|---|---|
| None | 只有 整數數字 元素。 |
| AllowDecimalPoint | 以及 。 以及 小數數字 元素。 |
| AllowExponent | 參數 s 也可以使用指數符號。 此旗標本身支援整 數數字E 指數位數的數值;為了成功解析指數符號中的字串,如正負符號及小數點符號,則需要額外的旗標。 |
| AllowLeadingWhite | 開頭的 s 元素。 |
| AllowTrailingWhite | 末尾的 s 元素。 |
| AllowLeadingSign | 符號 元素位於 的開頭 s。 |
| AllowTrailingSign | 符號 元素位於 的結尾 s。 |
| AllowParentheses | 符號 元素以 括號形式包含數值。 |
| AllowThousands | 這個元素。 |
| AllowCurrencySymbol | 元素 $ 。 |
| Currency | 都。 該 s 參數無法代表十六進位數或指數符號中的數字。 |
| Float | ws 元素位於 的 s開頭或結 尾,符號 位於 s,以及 。 符號。 參數 s 也可以使用指數符號。 |
| Number | 、wssign、千分隔符(,)和小數點(.)元素。 |
| Any | 除了 s 不能代表十六進位數字外,所有樣式都無法代表。 |
參數 provider 是一個 IFormatProvider 實作,其 GetFormat 方法回傳 NumberFormatInfo 一個物件,提供特定文化的格式資訊。 當 TryParse(String, NumberStyles, IFormatProvider, Single) 方法被呼叫時,它會呼叫 provider 參數的方法 GetFormat ,並傳入 Type 一個代表該型別的 NumberFormatInfo 物件。 接著,該 GetFormat 方法會回傳 NumberFormatInfo 提供參數格式 s 資訊的物件。 有三種方式可以利用該 provider 參數為解析操作提供自訂格式資訊:
你可以傳遞 CultureInfo 一個代表文化的物件,提供格式資訊。 其 GetFormat 方法回傳 NumberFormatInfo 提供該文化數值格式資訊的物件。
你可以傳遞提供數字格式資訊的實體 NumberFormatInfo 物件。 (它的實作 GetFormat 只是回傳自己。)
你可以傳遞一個自訂物件來實作 IFormatProvider。 其 GetFormat 方法實例化並提供格式資訊的 NumberFormatInfo 物件。
若 provider ,則 的null格式是根據s當前文化的NumberFormatInfo物件來解釋。
如果 s 超出資料 Single 型別範圍,該方法會在 .NET Framework 上拋出一個 OverflowException 。 在 .NET Core 3.0 及更新版本中,當 小於 且 Single.NegativeInfinitysSingle.MinValue 大於 Single.PositiveInfinity時,會回傳 s 。Single.MaxValue
若在解析操作中遇到參數分 s 隔符,且適用的貨幣或數字分隔符相同,則解析操作假設分隔符為十進位分隔符而非群分隔符。 欲了解更多分離子資訊,請參見 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparatorNumberGroupSeparator及 。