Single.Parse 方法

定義

將數字的字串表示轉換為其對等單精確度浮點數。

多載

Parse(String, NumberStyles, IFormatProvider)

將數字的字串表示 (使用指定樣式和的特定文化特性格式) 轉換為其對等單精確度浮點數。

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

將包含數字字串表示 (使用指定樣式和特定文化特性格式) 的字元範圍轉換為其對等單精確度浮點數。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

Parse(String, IFormatProvider)

將數字的字串表示 (使用指定的特定文化特性格式) 轉換為其對等單精確度浮點數。

Parse(String)

將數字的字串表示轉換為其對等單精確度浮點數。

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

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

Parse(String, NumberStyles)

將數字的字串表示 (使用指定樣式) 轉換為其對等單精確度浮點數。

備註

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

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 中可以存在的樣式項目。 所要指定一般值為 FloatAllowThousands 的組合。

provider
IFormatProvider

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

傳回

單精確度浮點數,其相當於 s 中指定的數值或符號。

實作

例外狀況

snull

s 不代表數字。

style 不是 NumberStyles 值。

-或-

styleAllowHexSpecifier 值。

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

參數 style 會定義樣式專案 (,例如空白字元、千位分隔符號和貨幣符號,) 參數中 s 允許的樣式專案,讓剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 不支援下列 NumberStyles 成員:

參數 s 可以包含 NumberFormatInfo.PositiveInfinitySymbolNumberFormatInfo.NegativeInfinitySymbolNumberFormatInfo.NaNSymbol 指定的文化特性 provider 。 視 的值 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 包含 旗標,參數 s 可以表示指數標記法的數位 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 開頭的 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 傳回 物件,這個物件提供 有關 格式 value 的文化特性特定資訊。 一般而言, provider 可以是下列任一項:

如果 為 providernull ,則會 NumberFormatInfo 使用目前文化特性的物件。

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

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

另請參閱

適用於

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 中可以存在的樣式項目。 所要指定一般值為 FloatAllowThousands 的組合。

provider
IFormatProvider

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

傳回

單精確度浮點數,其相當於 s 中指定的數值或符號。

實作

例外狀況

s 不代表數字。

style 不是 NumberStyles 值。

-或-

styleAllowHexSpecifier 值。

備註

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

如果 s 超出資料類型的範圍 Single ,則如果 s 小於 Single.MinValueSingle.PositiveInfinitysSingle.MaxValue 大於 ,則方法會傳回 。 Single.NegativeInfinity

適用於

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(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 中指定的數值或符號。

實作

例外狀況

snull

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

此多載通常用來將各種格式 Single 的文字轉換成值。 例如,它可以用來將使用者輸入的文字轉換成 HTML 文字方塊,轉換為數值。

參數 s 會使用 和 NumberStyles.AllowThousands 旗標的組合 NumberStyles.Float 來解譯。 參數 s 可以包含 NumberFormatInfo.PositiveInfinitySymbolNumberFormatInfo.NegativeInfinitySymbolNumberFormatInfo.NaNSymbol 指定之文化特性 provider ,也可以包含格式的字串:

[ws][sign]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws]

選擇性元素會以方括弧括住, ([ 和 ]) 。 包含「數位」一詞的專案包含一系列範圍從 0 到 9 的數位字元。

元素 描述
ws 一系列空白字元。
簽署 負號符號 ( ) 或正負號符號 (+) 。
整數數位 一系列的數位,範圍從 0 到 9,指定數位的整數部分。 整數數位的執行可由群組分隔符號符號分割。 例如,在某些文化特性中,逗號 (,) 會分隔千個群組。 如果字串包含fractional-digits元素,整數數位元素就可能不存在。
. 特定文化特性的小數點符號。
fractional-digits 一系列的數位,範圍從 0 到 9,指定數位的小數部分。
E 「e」 或 「E」 字元,表示該值是以指數 (科學) 標記法表示。
指數數位 一系列數位,範圍從 0 到 9,指定指數。

如需數值格式的詳細資訊,請參閱 格式化類型 主題。

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

如果 為 providernullNumberFormatInfo 無法取得 ,則會使用目前系統文化特性的格式資訊。

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

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

s 一些範例包括 「100」、「-123、456、789」、「123.45e+6」、「+500」、「5e2」、「3.1416」、「600.」、「-.123」 和 「-Infinity」。

另請參閱

適用於

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 中指定的數值或符號。

例外狀況

snull

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 IEEE 754 規格所需的或 NegativeInfinity 。 在舊版中,包括.NET Framework,剖析太大而無法表示的值會導致失敗。

參數 s 可以包含目前文化特性的 PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol 或 格式的字串:

[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.MinValuesSingle.PositiveInfinity 大於 Single.MaxValue ,則會傳回 Single.NegativeInfinity

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

另請參閱

適用於

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(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(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 中可以存在的樣式項目。 所要指定一般值為 FloatAllowThousands 的組合。

傳回

單精確度浮點數,其相當於 s 中指定的數值或符號。

例外狀況

snull

s 不是有效格式的數字。

僅.NET Framework和 .NET Core 2.2 和舊版: s 代表小於Single.MinValue 或大於 Single.MaxValue的數位。

style 不是 NumberStyles 值。

-或-

style 包含 AllowHexSpecifier 值。

範例

下列範例會 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 可以包含目前文化特性的 PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol 。 視 的值 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 開頭的 sws元素。
AllowTrailingWhite 結尾處的 sws元素。
AllowLeadingSign 開頭的 s符號專案。
AllowTrailingSign 結尾處的 s符號專案。
AllowParentheses 以括弧括住數值形式的 sign 元素。
AllowThousands 千位分隔符號 (,) 元素。
AllowCurrencySymbol 貨幣 ($) 元素。
Currency 所有元素。 不過, s 不能代表十六進位數或指數標記法中的數位。
Float 開頭或結尾的 sws元素,會在 開頭加上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.MinValuesSingle.PositiveInfinity 大於 Single.MaxValue ,則會傳回 Single.NegativeInfinity

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

另請參閱

適用於