閱讀英文

共用方式為


Double.Parse 方法

定義

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

多載

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

Parse(String)

來源:
Double.cs
來源:
Double.cs
來源:
Double.cs

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

C#
public static double Parse (string s);

參數

s
String

字串,包含要轉換的數位。

傳回

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

例外狀況

s 不代表有效格式的數位。

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

範例

下列範例說明如何使用 Parse(String) 方法。

C#
public class Temperature {
    // Parses the temperature from a string in form
    // [ws][sign]digits['F|'C][ws]
    public static Temperature Parse(string s) {
        Temperature temp = new Temperature();

        if( s.TrimEnd(null).EndsWith("'F") ) {
            temp.Value = Double.Parse( s.Remove(s.LastIndexOf('\''), 2) );
        }
        else if( s.TrimEnd(null).EndsWith("'C") ) {
            temp.Celsius = Double.Parse( s.Remove(s.LastIndexOf('\''), 2) );
        }
        else {
            temp.Value = Double.Parse(s);
        }

        return temp;
    }

    // The value holder
    protected double m_value;

    public double Value {
        get {
            return m_value;
        }
        set {
            m_value = value;
        }
    }

    public double Celsius {
        get {
            return (m_value-32.0)/1.8;
        }
        set {
            m_value = 1.8*value+32.0;
        }
    }
}

備註

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

s 參數可以包含目前文化特性的 NumberFormatInfo.PositiveInfinitySymbolNumberFormatInfo.NegativeInfinitySymbolNumberFormatInfo.NaNSymbol或格式的字串:

[ws][符號][整數數位[]]整數數位[.[小數位數]][E[符號]指數數位][ws]

方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。

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

s 參數是使用 NumberStyles.FloatNumberStyles.AllowThousands 旗標的組合來解譯。 這表示允許空格符和千位分隔符,例如,貨幣符號則不允許。 若要更精細地控制剖析作業在 s 允許哪些樣式專案成功,請呼叫 Double.Parse(String, NumberStyles)Double.Parse(String, NumberStyles, IFormatProvider) 方法。

s 參數是使用目前文化特性初始化之 NumberFormatInfo 物件中的格式信息來解譯。 如需詳細資訊,請參閱 CurrentInfo。 若要使用其他文化特性的格式資訊剖析字串,請呼叫 Double.Parse(String, IFormatProvider)Double.Parse(String, NumberStyles, IFormatProvider) 方法。

一般而言,如果您傳遞 Double.Parse 方法,則會傳回透過呼叫 Double.ToString 方法所建立的字串,則會傳回原始 Double 值。 不過,在 .NET Framework 和 .NET Core 2.2 和舊版上,由於精確度遺失,值可能不相等。 此外,嘗試剖析 Double.MinValueDouble.MaxValue 的字串表示無法往返。 在 .NET Framework 和 .NET Core 2.2 和舊版上,它會擲回 OverflowException。 下列範例提供圖例。

C#
   string value;

   value = Double.MinValue.ToString();
   try {
      Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException) {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }

   value = Double.MaxValue.ToString();
   try {
      Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException) {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }

   // Format without the default precision.
   value = Double.MinValue.ToString("G17");
   try
   {
       Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException)
   {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }
// The example displays the following output:
//    -1.79769313486232E+308 is outside the range of the Double type.
//    1.79769313486232E+308 is outside the range of the Double type.
//    -1.79769313486232E+308

在 .NET Framework 和 .NET Core 2.2 和舊版上,如果 s 超出 Double 數據類型的範圍,Parse(String) 方法會擲回 OverflowException

在 .NET Core 3.0 和更新版本上,當 s 超出 Double 數據類型的範圍時,不會擲回任何例外狀況。 在大部分情況下,方法會傳回 Double.PositiveInfinityDouble.NegativeInfinity。 不過,有一組小的值會被視為接近 Double 最大值或最小值,而不是正數或負無限大。 在這些情況下,方法會傳回 Double.MaxValueDouble.MinValue

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

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(ReadOnlySpan<Byte>, IFormatProvider)

來源:
Double.cs
來源:
Double.cs

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

C#
public static double Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的UTF-8字元範圍。

provider
IFormatProvider

物件,提供與 utf8Text相關的特定文化特性格式資訊。

傳回

剖析 utf8Text的結果。

實作

適用於

.NET 9 和 .NET 8
產品 版本
.NET 8, 9

Parse(ReadOnlySpan<Char>, IFormatProvider)

來源:
Double.cs
來源:
Double.cs
來源:
Double.cs

將字元範圍剖析為值。

C#
public static double Parse (ReadOnlySpan<char> s, IFormatProvider? provider);

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

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

傳回

剖析 s的結果。

實作

適用於

.NET 9 及其他版本
產品 版本
.NET 7, 8, 9

Parse(String, NumberStyles)

來源:
Double.cs
來源:
Double.cs
來源:
Double.cs

將指定樣式中數位的字串表示轉換為對等的雙精確度浮點數。

C#
public static double Parse (string s, System.Globalization.NumberStyles style);

參數

s
String

字串,包含要轉換的數位。

style
NumberStyles

列舉值的位元組合,表示 s中可以存在的樣式專案。 要指定的一般值是結合 FloatAllowThousands的組合。

傳回

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

例外狀況

s 不代表有效格式的數位。

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

style 不是 NumberStyles 值。

-或-

style 包含 AllowHexSpecifier 值。

範例

下列範例使用 Parse(String, NumberStyles) 方法來使用 en-US 文化特性剖析 Double 值的字串表示。

C#
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)
{
   double number;
   try
   {
      number = Double.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.

備註

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

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

s 參數可以包含目前文化特性的 NumberFormatInfo.PositiveInfinitySymbolNumberFormatInfo.NegativeInfinitySymbolNumberFormatInfo.NaNSymbol。 視 style的值而定,也可以採用下列格式:

[ws][$][符號][整數數位[]]整數數位[.[分數數位]][E[符號]指數數位][ws]

方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。

元素 描述
ws 一系列空格符。 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,則會在 s 開頭顯示空格符,如果 style 包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 s 結尾。
$ 特定文化特性的貨幣符號。 字串中的位置是由目前文化特性的 NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern 屬性所定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前的文化特性貨幣符號可能會出現在 s 中。
簽署 負號符號 (-) 或正負號符號 (+)。 如果 style 包含 NumberStyles.AllowLeadingSign 旗標,則符號可能會出現在 s 開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,則它可能會出現在 s 結尾。 括弧可用於 s,如果 style 包含 NumberStyles.AllowParentheses 旗標,則表示負值。
整數數位 一系列數位,範圍從 0 到 9,指定數位的整數部分。 如果字串包含 小數位數 元素,則 整數數位 元素可能不存在。
, 特定文化特性的群組分隔符。 如果 style 包含 NumberStyles.AllowThousands 旗標,則目前文化特性的群組分隔符符號可能會出現在 s
. 特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可能會出現在 s 中。
小數位數 一系列數位,範圍從 0 到 9,指定數位的小數部分。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則小數字數可以出現在 s 中。
E “e” 或 “E” 字元,表示值是以指數(科學)表示法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數就可以以指數表示法來表示數位。
指數數位 指定指數的一系列數位範圍從 0 到 9。

注意

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

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

NumberStyles 值 除了數位以外,s 中允許的專案
None 整數數位 元素。
AllowDecimalPoint 小數點(.)和 小數位數 元素。
AllowExponent 表示指數表示法的 「e」 或 「E」 字元。 此旗標本身支援以 E數位 格式的值;需要額外的旗標,才能成功剖析具有正負號和小數點符號等元素的字串。
AllowLeadingWhite s開頭的 ws ws 專案。
AllowTrailingWhite s結尾的 ws 專案。
AllowLeadingSign s開頭的 符號 專案。
AllowTrailingSign s結尾處的 符號 專案。
AllowParentheses 以括弧括住數值的括弧形式,符號 專案。
AllowThousands 千位分隔符 (,) 元素。
AllowCurrencySymbol currency ($) 元素。
Currency 所有元素。 不過,s 不能代表十六進位數或指數表示法的數位。
Float s開頭或結尾的 ws 專案,符號s開頭,以及小數點 (.) 符號。 s 參數也可以使用指數表示法。
Number wssign、千位分隔符 (、) 和小數點 (.) 元素。
Any 所有元素。 不過,s 不能代表十六進位數。

s 參數是使用目前系統文化特性初始化之 NumberFormatInfo 物件中的格式資訊來剖析。 如需詳細資訊,請參閱 CurrentInfo

一般而言,如果您傳遞 Double.Parse 方法,則會傳回透過呼叫 Double.ToString 方法所建立的字串,則會傳回原始 Double 值。 不過,由於精確度遺失,值可能不相等。 此外,嘗試剖析 Double.MinValueDouble.MaxValue 的字串表示無法往返。 在 .NET Framework 和 .NET Core 2.2 和舊版上,它會擲回 OverflowException。 在 .NET Core 3.0 和更新版本上,如果您嘗試剖析 MinValue 或嘗試剖析 MaxValue,則會傳回 Double.PositiveInfinityDouble.NegativeInfinity。 下列範例提供圖例。

C#
   string value;

   value = Double.MinValue.ToString();
   try {
      Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException) {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }

   value = Double.MaxValue.ToString();
   try {
      Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException) {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }

   // Format without the default precision.
   value = Double.MinValue.ToString("G17");
   try
   {
       Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException)
   {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }
// The example displays the following output:
//    -1.79769313486232E+308 is outside the range of the Double type.
//    1.79769313486232E+308 is outside the range of the Double type.
//    -1.79769313486232E+308

在 .NET Framework 和 .NET Core 2.2 和舊版上,如果 s 超出 Double 數據類型的範圍,Parse(String, NumberStyles) 方法會擲回 OverflowException

在 .NET Core 3.0 和更新版本上,當 s 超出 Double 數據類型的範圍時,不會擲回任何例外狀況。 在大部分情況下,Parse(String, NumberStyles) 方法會傳回 Double.PositiveInfinityDouble.NegativeInfinity。 不過,有一組小的值會被視為接近 Double 最大值或最小值,而不是正數或負無限大。 在這些情況下,方法會傳回 Double.MaxValueDouble.MinValue

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

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(String, IFormatProvider)

來源:
Double.cs
來源:
Double.cs
來源:
Double.cs

將指定文化特性特定格式之數位的字串表示轉換為其對等的雙精確度浮點數。

C#
public static double Parse (string s, IFormatProvider provider);
C#
public static double Parse (string s, IFormatProvider? provider);

參數

s
String

字串,包含要轉換的數位。

provider
IFormatProvider

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

傳回

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

實作

例外狀況

s 不代表有效格式的數位。

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

範例

下列範例是 Web 窗體的按鈕點選事件處理程式。 它會使用 HttpRequest.UserLanguages 屬性所傳回的數位來判斷使用者的地區設定。 然後,它會具現化對應至該地區設定的 CultureInfo 物件。 屬於該 CultureInfo 物件的 NumberFormatInfo 對象接著會傳遞至 Parse(String, IFormatProvider) 方法,將使用者的輸入轉換成 Double 值。

C#
protected void OkToDouble_Click(object sender, EventArgs e)
{
    string locale;
    double 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 = Double.Parse(this.inputNumber.Text, culture.NumberFormat);
    }
    catch (FormatException)
    {
        return;
    }
    catch (OverflowException)
    {
        return;
    }
    // Output number to label on web form
    this.outputNumber.Text = "Number is " + number.ToString();
}

備註

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

這個 Parse(String, IFormatProvider) 方法的多載通常用來將各種格式的文字轉換成 Double 值。 例如,它可以用來將使用者輸入的文字轉換成 HTML 文字框,轉換為數值。

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

[ws][符號]整數數位[.[分數數位]][E[符號]指數數位][ws]

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

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

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

provider 參數是 IFormatProvider 實作,其 GetFormat 方法會傳回 NumberFormatInfo 物件,以提供解譯 s格式時所使用的特定文化特性資訊。 一般而言,它是 NumberFormatInfoCultureInfo 物件。 如果 providernull 或無法取得 NumberFormatInfo,則會使用目前系統文化特性的格式資訊。

一般而言,如果您傳遞 Double.Parse 方法,則會傳回透過呼叫 Double.ToString 方法所建立的字串,則會傳回原始 Double 值。 不過,由於精確度遺失,值可能不相等。 此外,嘗試剖析 Double.MinValueDouble.MaxValue 的字串表示無法往返。 在 .NET Framework 和 .NET Core 2.2 和舊版上,它會擲回 OverflowException。 在 .NET Core 3.0 和更新版本上,如果您嘗試剖析 MinValue 或嘗試剖析 MaxValue,則會傳回 Double.PositiveInfinityDouble.NegativeInfinity。 下列範例提供圖例。

C#
   string value;

   value = Double.MinValue.ToString();
   try {
      Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException) {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }

   value = Double.MaxValue.ToString();
   try {
      Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException) {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }

   // Format without the default precision.
   value = Double.MinValue.ToString("G17");
   try
   {
       Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException)
   {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }
// The example displays the following output:
//    -1.79769313486232E+308 is outside the range of the Double type.
//    1.79769313486232E+308 is outside the range of the Double type.
//    -1.79769313486232E+308

在 .NET Framework 和 .NET Core 2.2 和舊版上,如果 s 超出 Double 數據類型的範圍,Parse(String, IFormatProvider) 方法會擲回 OverflowException

在 .NET Core 3.0 和更新版本上,當 s 超出 Double 數據類型的範圍時,不會擲回任何例外狀況。 在大部分情況下,Parse(String, IFormatProvider) 方法會傳回 Double.PositiveInfinityDouble.NegativeInfinity。 不過,有一組小的值會被視為接近 Double 最大值或最小值,而不是正數或負無限大。 在這些情況下,方法會傳回 Double.MaxValueDouble.MinValue

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

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

來源:
Double.cs
來源:
Double.cs

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

C#
public static double Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, IFormatProvider? provider = default);

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的UTF-8字元範圍。

style
NumberStyles

數字樣式的位元組合,可以存在於 utf8Text中。

provider
IFormatProvider

物件,提供與 utf8Text相關的特定文化特性格式資訊。

傳回

剖析 utf8Text的結果。

實作

適用於

.NET 9 和 .NET 8
產品 版本
.NET 8, 9

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

來源:
Double.cs
來源:
Double.cs
來源:
Double.cs

將字元範圍,其中包含指定樣式和特定文化特性格式之數位的字串表示,轉換為其對等的雙精確度浮點數。

C#
public static double Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, IFormatProvider? provider = default);
C#
public static double Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, IFormatProvider provider = default);

參數

s
ReadOnlySpan<Char>

包含要轉換之數位的字元範圍。

style
NumberStyles

列舉值的位元組合,表示 s中可以存在的樣式專案。 要指定的一般值會與 AllowThousands結合 Float

provider
IFormatProvider

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

傳回

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

實作

例外狀況

s 不代表數值。

style 不是 NumberStyles 值。

-或-

styleAllowHexSpecifier 值。

備註

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

如果 s 超出 Double 數據類型的範圍,則如果 s 小於 Double.MinValue,則方法會傳回 Double.NegativeInfinity;如果 s 大於 Double.MaxValue,則 Double.PositiveInfinity

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Parse(String, NumberStyles, IFormatProvider)

來源:
Double.cs
來源:
Double.cs
來源:
Double.cs

將指定樣式和特定文化特性格式之數位的字串表示轉換為其對等的雙精確度浮點數。

C#
public static double Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
C#
public static double Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);

參數

s
String

字串,包含要轉換的數位。

style
NumberStyles

列舉值的位元組合,表示 s中可以存在的樣式專案。 要指定的一般值會與 AllowThousands結合 Float

provider
IFormatProvider

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

傳回

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

實作

例外狀況

s 不代表數值。

style 不是 NumberStyles 值。

-或-

styleAllowHexSpecifier 值。

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

範例

下列範例說明如何使用 Parse(String, NumberStyles, IFormatProvider) 方法,將數個溫度值的字串表示指派給 Temperature 物件。

C#
using System;
using System.Globalization;

public class Temperature
{
   // Parses the temperature from a string. Temperature scale is
   // indicated by 'F (for Fahrenheit) or 'C (for Celsius) at the end
   // of the string.
   public static Temperature Parse(string s, NumberStyles styles,
                                   IFormatProvider provider)
   {
      Temperature temp = new Temperature();

      if (s.TrimEnd(null).EndsWith("'F"))
      {
         temp.Value = Double.Parse(s.Remove(s.LastIndexOf((char)39), 2),
                                   styles, provider);
      }
      else
      {
         if (s.TrimEnd(null).EndsWith("'C"))
            temp.Celsius = Double.Parse(s.Remove(s.LastIndexOf((char)39), 2),
                                        styles, provider);
         else
            temp.Value = Double.Parse(s, styles, provider);
      }
      return temp;
   }

   // Declare private constructor so Temperature so only Parse method can
   // create a new instance
   private Temperature()   {}

   protected double m_value;

   public double Value
   {
      get { return m_value; }
      private set { m_value = value; }
   }

   public double Celsius
   {
      get { return (m_value - 32) / 1.8; }
      private set { m_value = value * 1.8 + 32; }
   }

   public double Fahrenheit
   {
      get {return m_value; }
   }
}

public class TestTemperature
{
   public static void Main()
   {
      string value;
      NumberStyles styles;
      IFormatProvider provider;
      Temperature temp;

      value = "25,3'C";
      styles = NumberStyles.Float;
      provider = CultureInfo.CreateSpecificCulture("fr-FR");
      temp = Temperature.Parse(value, styles, provider);
      Console.WriteLine("{0} degrees Fahrenheit equals {1} degrees Celsius.",
                        temp.Fahrenheit, temp.Celsius);

      value = " (40) 'C";
      styles = NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite
               | NumberStyles.AllowParentheses;
      provider = NumberFormatInfo.InvariantInfo;
      temp = Temperature.Parse(value, styles, provider);
      Console.WriteLine("{0} degrees Fahrenheit equals {1} degrees Celsius.",
                        temp.Fahrenheit, temp.Celsius);

      value = "5,778E03'C";      // Approximate surface temperature of the Sun
      styles = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands |
               NumberStyles.AllowExponent;
      provider = CultureInfo.CreateSpecificCulture("en-GB");
      temp = Temperature.Parse(value, styles, provider);
      Console.WriteLine("{0} degrees Fahrenheit equals {1} degrees Celsius.",
                        temp.Fahrenheit.ToString("N"), temp.Celsius.ToString("N"));
   }
}

備註

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

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

s 參數可以包含 provider所指定文化特性的 NumberFormatInfo.PositiveInfinitySymbolNumberFormatInfo.NegativeInfinitySymbolNumberFormatInfo.NaNSymbol。 視 style的值而定,也可以採用下列格式:

[ws][$][符號][整數數位,]整數數位[.[分數數位]][E[符號]指數數位][ws]

以方括弧 ([ 和 ]) 框架的項目是選擇性的。 下表描述每個元素。

元素 描述
ws 一系列空格符。 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,則會在 s 開頭顯示空格符,如果 style 包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 s 結尾。
$ 特定文化特性的貨幣符號。 字串中的位置是由目前文化特性的 NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern 屬性所定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前的文化特性貨幣符號可能會出現在 s 中。
簽署 負號符號 (-) 或正負號符號 (+)。 如果 style 包含 NumberStyles.AllowLeadingSign 旗標,則符號可能會出現在 s 開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,則它可能會出現在 s 結尾。 括弧可用於 s,如果 style 包含 NumberStyles.AllowParentheses 旗標,則表示負值。
整數數位 一系列數位,範圍從 0 到 9,指定數位的整數部分。 如果字串包含 小數位數 元素,則 整數數位 元素可能不存在。
, 特定文化特性的群組分隔符。 如果 style 包含 NumberStyles.AllowThousands 旗標,則目前文化特性的群組分隔符符號可能會出現在 s
. 特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可能會出現在 s 中。
小數位數 一系列數位,範圍從 0 到 9,指定數位的小數部分。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則小數字數可以出現在 s 中。
E “e” 或 “E” 字元,表示值是以指數(科學)表示法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數就可以以指數表示法來表示數位。
指數數位 指定指數的一系列數位範圍從 0 到 9。

注意

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

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

NumberStyles 值 除了數位以外,s 中允許的專案
None 整數數位 元素。
AllowDecimalPoint 小數點(.)和 小數位數 元素。
AllowExponent 表示指數表示法的 「e」 或 「E」 字元。 此旗標本身支援以 E數位 格式的值;需要額外的旗標,才能成功剖析具有正負號和小數點符號等元素的字串。
AllowLeadingWhite s開頭的 ws ws 專案。
AllowTrailingWhite s結尾的 ws 專案。
AllowLeadingSign s開頭的 符號 專案。
AllowTrailingSign s結尾處的 符號 專案。
AllowParentheses 以括弧括住數值的括弧形式,符號 專案。
AllowThousands 千位分隔符 (,) 元素。
AllowCurrencySymbol currency ($) 元素。
Currency 所有元素。 不過,s 不能代表十六進位數或指數表示法的數位。
Float s開頭或結尾的 ws 專案,符號s開頭,以及小數點 (.) 符號。 s 參數也可以使用指數表示法。
Number wssign、千位分隔符 (、) 和小數點 (.) 元素。
Any 所有元素。 不過,s 不能代表十六進位數。

provider 參數是 IFormatProvider 實作,其 GetFormat 方法會傳回 NumberFormatInfo 物件,以提供解譯 s格式時所使用的特定文化特性資訊。 一般而言,它是 NumberFormatInfoCultureInfo 物件。 如果 providernull 或無法取得 NumberFormatInfo,則會使用目前系統文化特性的格式資訊。

一般而言,如果您傳遞 Double.Parse 方法,則會傳回透過呼叫 Double.ToString 方法所建立的字串,則會傳回原始 Double 值。 不過,由於精確度遺失,值可能不相等。 此外,嘗試剖析 MinValueDouble.MaxValue 的字串表示無法往返。 在 .NET Framework 和 .NET Core 2.2 和舊版上,它會擲回 OverflowException。 在 .NET Core 3.0 和更新版本上,如果您嘗試剖析 MinValue 或嘗試剖析 MaxValue,則會傳回 Double.PositiveInfinityDouble.NegativeInfinity。 下列範例提供圖例。

C#
   string value;

   value = Double.MinValue.ToString();
   try {
      Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException) {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }

   value = Double.MaxValue.ToString();
   try {
      Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException) {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }

   // Format without the default precision.
   value = Double.MinValue.ToString("G17");
   try
   {
       Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException)
   {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }
// The example displays the following output:
//    -1.79769313486232E+308 is outside the range of the Double type.
//    1.79769313486232E+308 is outside the range of the Double type.
//    -1.79769313486232E+308

在 .NET Framework 和 .NET Core 2.2 和舊版上,如果 s 超出 Double 數據類型的範圍,Parse(String, NumberStyles, IFormatProvider) 方法會擲回 OverflowException

在 .NET Core 3.0 和更新版本上,當 s 超出 Double 數據類型的範圍時,不會擲回任何例外狀況。 在大部分情況下,Parse(String, NumberStyles, IFormatProvider) 方法會傳回 Double.PositiveInfinityDouble.NegativeInfinity。 不過,有一組小的值會被視為接近 Double 最大值或最小值,而不是正數或負無限大。 在這些情況下,方法會傳回 Double.MaxValueDouble.MinValue

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

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0