Single.TryParse 方法

定義

將數字的字串表示轉換為其對等單精確度浮點數。 傳回值,該值指出轉換成功或失敗。

多載

TryParse(ReadOnlySpan<Char>, IFormatProvider, Single)

嘗試將字元範圍剖析為值。

TryParse(ReadOnlySpan<Char>, Single)

將數字的字串表示 (使用字串範圍) 轉換為其對等單精確度浮點數。 傳回值,該值指出轉換成功或失敗。

TryParse(String, Single)

將數字的字串表示轉換為其對等單精確度浮點數。 傳回值,該值指出轉換成功或失敗。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Single)

嘗試將 UTF-8 字元的範圍剖析為值。

TryParse(String, 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 和更新版本中,無法表示的值會四捨五入或 PositiveInfinityNegativeInfinity IEEE 754 規格所需的值。 在舊版中,包括.NET Framework,剖析太大而無法表示的值會導致失敗。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Single)

來源:
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<Char>, Single)

來源:
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 中原始提供的任何值都將遭到覆寫。

傳回

如果 s 轉換成功,則為 true,否則為 false

備註

在 .NET Core 3.0 和更新版本中,太大而無法表示的值會四捨五入為 PositiveInfinity IEEE 754 規格所需的或 NegativeInfinity 。 在舊版中,包括.NET Framework,剖析太大而無法表示的值會導致失敗。

適用於

TryParse(String, Single)

來源:
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,或者不是有效格式的數字,則轉換會失敗。 如果 代表小於Single.MinValue 或大於 Single.MaxValue的數位,.NET Framework s 和 .NET Core 2.2 和更早版本也會失敗。 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。

傳回

如果 s 轉換成功,則為 true,否則為 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 IEEE 754 規格所需的或 NegativeInfinity 。 在舊版中,包括.NET Framework,剖析太大而無法表示的值會導致失敗。

這個多載與 方法不同 Single.Parse(String) ,方法是傳回布林值,指出剖析作業是否成功,而不是傳回剖析的數值。 它不需要使用例外狀況處理,在無效且無法成功剖析的 事件 s 中測試 FormatException

參數 s 可以包含 PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol (字串比較區分大小寫) 或格式的字串:

[ws][sign][integral-digits,]integral-digits[.[fractional-digits][e[sign]exponential-digits][ws]

在方括號中的元素是選擇性的元素。 下表說明每個元素。

元素 描述
ws 一系列空白字元。
簽署 負號或正負號符號。
整數數位 一系列數值字元,範圍從 0 到 9,指定數位的整數部分。 如果有小數位數,整數數位可能不存在。
, 特定文化特性的群組分隔符號符號。
. 特定文化特性的小數點符號。
fractional-digits 一連串的數值字元,範圍從 0 到 9,指定數位的小數部分。
E 大寫或小寫字元 'e',表示指數 (科學) 標記法。
指數數位 一系列的數值字元,範圍從 0 到 9,指定指數。

參數 s 會使用 和 NumberStyles.AllowThousands 旗標的組合 NumberStyles.Float 來解譯。 這表示允許空白字元和千位分隔符號,但貨幣符號則不允許。 若要明確定義 (專案,例如貨幣符號、千位分隔符號和空白字元,) 可以存在於 中 s ,請使用 TryParse(String, NumberStyles, IFormatProvider, Single) 方法多載。

參數 s 會使用針對目前系統文化特性初始化的 物件中的 NumberFormatInfo 格式資訊進行剖析。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo。 若要使用某些其他指定文化特性的格式資訊剖析字串,請使用 TryParse(String, NumberStyles, IFormatProvider, Single) 方法多載。

一般而言,如果您傳遞 Single.TryParse 方法以呼叫 方法所建立的 Single.ToString 字串,則會傳回原始 Single 值。 不過,由於遺失精確度,值可能不相等。

如果 s 超出資料類型的範圍 Single ,則方法會在 false .NET Framework 和 .NET Core 2.2 和舊版傳回。 在 .NET Core 3.0 和更新版本上,如果 s 小於 Single.MinValuesSingle.PositiveInfinity 大於 Single.MaxValue ,則會傳回 Single.NegativeInfinity

如果在剖析作業期間于 參數中 s 遇到分隔符號,且適用的貨幣或數位十進位和群組分隔符號相同,則剖析作業會假設分隔符號是小數分隔符號,而不是群組分隔符號。 如需分隔符號的詳細資訊,請參閱 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator

另請參閱

適用於

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Single)

來源:
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(String, IFormatProvider, Single)

來源:
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<Byte>, NumberStyles, IFormatProvider, Single)

來源:
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

嘗試將包含數位字串表示的 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 或 不是有效格式,轉換就會失敗。 此參數會以未初始化的狀態來傳遞,並會覆寫任何原本在結果中提供的值。

傳回

如果 utf8Text 轉換成功,則為 true,否則為 false

適用於

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single)

來源:
Single.cs
來源:
Single.cs
來源:
Single.cs

將數字的範圍表示 (使用指定樣式和特定文化特性格式) 轉換為其對等單精確度浮點數。 傳回值,該值指出轉換成功或失敗。

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, [Runtime::InteropServices::Out] float % result) = System::Numerics::INumberBase<float>::TryParse;
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 所允許的格式。 所要指定一般值為 FloatAllowThousands 的組合。

provider
IFormatProvider

物件,其提供關於 s 的特定文化特性格式資訊。

result
Single

當這個方法傳回時,如果轉換成功,則包含相當於 s 中所包含數值或符號的單精確度浮點數;如果轉換失敗,則為零。 如果 s 參數為 nullEmpty ,且格式不符合 style ,則轉換會失敗,表示小於Single.MinValue 或大於 Single.MaxValue的數位,如果style 不是列舉常數的有效組合 NumberStyles ,則轉換會失敗。 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。

傳回

如果 s 轉換成功,則為 true,否則為 false

備註

在 .NET Core 3.0 和更新版本中,無法表示的值會四捨五入或 PositiveInfinityNegativeInfinity IEEE 754 規格所需的值。 在舊版中,包括.NET Framework,剖析太大而無法表示的值會導致失敗。

適用於

TryParse(String, NumberStyles, IFormatProvider, Single)

來源:
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 所允許的格式。 所要指定一般值為 FloatAllowThousands 的組合。

provider
IFormatProvider

物件,其提供關於 s 的特定文化特性格式資訊。

result
Single

當這個方法傳回時,如果轉換成功,則包含相當於 s 中所包含數值或符號的單精確度浮點數;如果轉換失敗,則為零。 如果 s 參數為 nullEmpty、格式不符合 style 規範,或者 style 不是 NumberStyles 列舉常數的有效組合,則轉換會失敗。 如果 s 代表小於Single.MinValue 或大於 Single.MaxValue的數位,它也會在 .NET Framework 或 .NET Core 2.2 和舊版上失敗。 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。

傳回

如果 s 轉換成功,則為 true,否則為 false

例外狀況

style 不是 NumberStyles 值。

-或-

styleAllowHexSpecifier 值。

範例

下列範例示範如何使用 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 和更新版本中,無法表示的值會四捨五入或 PositiveInfinityNegativeInfinity IEEE 754 規格所需的值。 在舊版中,包括.NET Framework,剖析太大而無法表示的值會導致失敗。

這個多載與 Parse(String, NumberStyles, IFormatProvider) 方法不同,方法是傳回布林值,指出剖析作業是否成功,而不是傳回剖析的數值。 它不需要使用例外狀況處理來測試 FormatException 事件 s 中無效且無法成功剖析的 。

參數 style 會定義參數的允許格式 s ,讓剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 不支援下列 NumberStyles 成員:

參數 s 可以包含 PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol 表示的文化特性 provider 。 此外,根據 的值 styles 參數可能包含下列元素:

[ws][$][sign][integral-digits,]integral-digits[.fractional-digits][e[sign]exponential-digits][ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。

元素 描述
ws 選擇性空白字元。 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,則空白字元可以出現在 的 s 開頭。 如果 style 包含 旗標, NumberStyles.AllowTrailingWhite 它可能會出現在 結尾 s
$ 特定文化特性的貨幣符號。 字串中的位置是由 NumberFormatInfo.CurrencyNegativePattern 參數的 方法 provider 所傳回之 物件的 或 NumberFormatInfo.CurrencyPositivePattern 屬性 NumberFormatInfoIFormatProvider.GetFormat 定義。 如果 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 字元,表示 s 可以使用指數標記法來表示數位。 如果樣式包含 旗標,參數 s 可以表示指數標記法的數位 NumberStyles.AllowExponent
指數數位 指定指數的一系列數位範圍從 0 到 9。

注意

不論引數的值 style 為何,剖析作業都會忽略 中 s 任何終止的 NUL (U+0000) 字元。

只有數位的字串 (對應至 NumberStyles.None 樣式) 在類型範圍內 Single 時一律會成功剖析。 其餘 System.Globalization.NumberStyles 成員控制項元素可能存在,但不需要出現在輸入字串中。 下表指出個別 NumberStyles 旗標如何影響 中 s 可能存在的專案。

NumberStyles 值 除了數位之外,也允許的專案
None 僅限 整數數位元素
AllowDecimalPoint 小數位數元素
AllowExponent 參數 s 也可以使用指數標記法。 這個旗標本身支援 整數數位E指數數位格式的值;需要額外的旗標,才能使用正數或負號和小數點符號等元素,成功剖析指數標記法中的字串。
AllowLeadingWhite 開頭的 sws元素。
AllowTrailingWhite 結尾處的 sws元素。
AllowLeadingSign 開頭的 s符號專案。
AllowTrailingSign 結尾處的 s符號專案。
AllowParentheses 以括弧括住數值形式的 sign 元素。
AllowThousands 元素。
AllowCurrencySymbol $ 項目。
Currency 全部。 參數 s 不能代表十六進位數或指數標記法中的數位。
Float 開頭或結尾的 sws元素,s 開頭和符號。 參數 s 也可以使用指數標記法。
Number wssign 、thousands 分隔符號 (、) 和小數點 ()
Any 除了 以外的 s 所有樣式都不能代表十六進位數。

參數 provider 是實作 IFormatProvider ,其 GetFormat 方法會 NumberFormatInfo 傳回提供特定文化特性格式資訊的 物件。 TryParse(String, NumberStyles, IFormatProvider, Single)叫用 方法時,它會呼叫 provider 參數的 方法,並傳遞 Type 代表 NumberFormatInfo 型別的 GetFormat 物件。 GetFormat然後,方法會 NumberFormatInfo 傳回 物件,提供參數格式 s 的相關資訊。 有三種方式可以使用 provider 參數,將自訂格式資訊提供給剖析作業:

如果 providernull ,則會 s 根據 NumberFormatInfo 目前文化特性的物件來解譯 的格式。

如果 s 超出資料類型的範圍 Single ,方法會在 .NET Framework 和 .NET Core 2.2 和舊版上擲回 OverflowException 。 在 .NET Core 3.0 和更新版本上,如果 s 小於 Single.MinValuesSingle.PositiveInfinity 大於 Single.MaxValue ,則會傳回 Single.NegativeInfinity

如果在剖析作業期間于 參數中 s 遇到分隔符號,且適用的貨幣或數位十進位和群組分隔符號相同,則剖析作業會假設分隔符號是小數分隔符號,而不是群組分隔符號。 如需分隔符號的詳細資訊,請參閱 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparator 、 和 NumberGroupSeparator

另請參閱

適用於