共用方式為


Complex.ToString 方法

定義

將複數的值轉換為其相等的字串表示。

多載

ToString(String, IFormatProvider)

使用指定的格式和文化特性特定格式資訊,將目前複數的值轉換為其以笛卡兒形式表示的對等字串表示法,以實數和虛數部分。

ToString(String)

使用指定的格式,將目前複數的值,轉換為其實數和虛數部分的相等字串表示。

ToString(IFormatProvider)

使用指定的特定文化特性格式資訊,將目前複數的值轉換為其相等字串表示法。

ToString()

將目前複數的值轉換為以笛卡兒形式表示的對等字串表示。

ToString(String, IFormatProvider)

來源:
Complex.cs
來源:
Complex.cs
來源:
Complex.cs

使用指定的格式和文化特性特定格式資訊,將目前複數的值轉換為其以笛卡兒形式表示的對等字串表示法,以實數和虛數部分。

public:
 virtual System::String ^ ToString(System::String ^ format, IFormatProvider ^ provider);
public string ToString (string format, IFormatProvider provider);
public string ToString (string? format, IFormatProvider? provider);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, provider As IFormatProvider) As String

參數

format
String

標準或自定義數值格式字串。

provider
IFormatProvider

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

傳回

以笛卡兒形式表示目前實例的字串表示,如 formatprovider所指定。

實作

例外狀況

format 不是有效的格式字串。

範例

下列範例會建立複數數位列,並使用數個標準格式字串以及代表英文 - 美國(“en-US”) 和法文 - 法國 (“fr-FR”) 文化特性的 CultureInfo 物件來顯示每一個。

using System;
using System.Globalization;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      Complex[] c = { new Complex(17.3, 14.1),
                      new Complex(-18.9, 147.2),
                      new Complex(13.472, -18.115),
                      new Complex(-11.154, -17.002) };
      CultureInfo[] cultures = { new CultureInfo("en-US"),
                                 new CultureInfo("fr-FR") };
      string[] formats = { "F2", "N2", "G5" };

      foreach (Complex c1 in c)
      {
         foreach (string format in formats)
         {
            Console.Write("{0} format string:   ", format);
            foreach (CultureInfo culture in cultures)
               Console.Write("{0} ({1})    ", c1.ToString(format, culture), culture.Name);

            Console.WriteLine();
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//    F2 format string:   (17.30, 14.10) (en-US)    (17,30, 14,10) (fr-FR)
//    N2 format string:   (17.30, 14.10) (en-US)    (17,30, 14,10) (fr-FR)
//    G5 format string:   (17.3, 14.1) (en-US)    (17,3, 14,1) (fr-FR)
//
//    F2 format string:   (-18.90, 147.20) (en-US)    (-18,90, 147,20) (fr-FR)
//    N2 format string:   (-18.90, 147.20) (en-US)    (-18,90, 147,20) (fr-FR)
//    G5 format string:   (-18.9, 147.2) (en-US)    (-18,9, 147,2) (fr-FR)
//
//    F2 format string:   (13.47, -18.12) (en-US)    (13,47, -18,12) (fr-FR)
//    N2 format string:   (13.47, -18.12) (en-US)    (13,47, -18,12) (fr-FR)
//    G5 format string:   (13.472, -18.115) (en-US)    (13,472, -18,115) (fr-FR)
//
//    F2 format string:   (-11.15, -17.00) (en-US)    (-11,15, -17,00) (fr-FR)
//    N2 format string:   (-11.15, -17.00) (en-US)    (-11,15, -17,00) (fr-FR)
//    G5 format string:   (-11.154, -17.002) (en-US)    (-11,154, -17,002) (fr-FR)
open System.Globalization
open System.Numerics

let c =
    [ Complex(17.3, 14.1)
      Complex(-18.9, 147.2)
      Complex(13.472, -18.115)
      Complex(-11.154, -17.002) ]

let cultures = [ CultureInfo "en-US"; CultureInfo "fr-FR" ]
let formats = [ "F2"; "N2"; "G5" ]

for c1 in c do
    for format in formats do
        for culture in cultures do
            printf $"{format} format string: {c1.ToString(format, culture)} ({culture.Name})    "

        printfn ""

    printfn ""
// The example displays the following output:
//    F2 format string:   (17.30, 14.10) (en-US)    (17,30, 14,10) (fr-FR)
//    N2 format string:   (17.30, 14.10) (en-US)    (17,30, 14,10) (fr-FR)
//    G5 format string:   (17.3, 14.1) (en-US)    (17,3, 14,1) (fr-FR)
//
//    F2 format string:   (-18.90, 147.20) (en-US)    (-18,90, 147,20) (fr-FR)
//    N2 format string:   (-18.90, 147.20) (en-US)    (-18,90, 147,20) (fr-FR)
//    G5 format string:   (-18.9, 147.2) (en-US)    (-18,9, 147,2) (fr-FR)
//
//    F2 format string:   (13.47, -18.12) (en-US)    (13,47, -18,12) (fr-FR)
//    N2 format string:   (13.47, -18.12) (en-US)    (13,47, -18,12) (fr-FR)
//    G5 format string:   (13.472, -18.115) (en-US)    (13,472, -18,115) (fr-FR)
//
//    F2 format string:   (-11.15, -17.00) (en-US)    (-11,15, -17,00) (fr-FR)
//    N2 format string:   (-11.15, -17.00) (en-US)    (-11,15, -17,00) (fr-FR)
//    G5 format string:   (-11.154, -17.002) (en-US)    (-11,154, -17,002) (fr-FR)
Imports System.Globalization
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim c() As Complex = { New Complex(17.3, 14.1), 
                             New Complex(-18.9, 147.2), 
                             New Complex(13.472, -18.115), 
                             New Complex(-11.154, -17.002) }
      Dim cultures() As CultureInfo = { New CultureInfo("en-US"), 
                                        New CultureInfo("fr-FR") } 
      Dim formats() As String = { "F2", "N2", "G5" } 

      For Each c1 As Complex In c
         For Each format As String In formats
            Console.Write("{0} format string:   ", format)
            For Each culture As CultureInfo In cultures
               Console.Write("{0} ({1})    ", c1.ToString(format, culture), 
                                              culture.Name)
            Next
            Console.WriteLine()
         Next
         Console.WriteLine()
      Next                          
   End Sub
End Module
' The example displays the following output:
'    F2 format string:   (17.30, 14.10) (en-US)    (17,30, 14,10) (fr-FR)
'    N2 format string:   (17.30, 14.10) (en-US)    (17,30, 14,10) (fr-FR)
'    G5 format string:   (17.3, 14.1) (en-US)    (17,3, 14,1) (fr-FR)
'    
'    F2 format string:   (-18.90, 147.20) (en-US)    (-18,90, 147,20) (fr-FR)
'    N2 format string:   (-18.90, 147.20) (en-US)    (-18,90, 147,20) (fr-FR)
'    G5 format string:   (-18.9, 147.2) (en-US)    (-18,9, 147,2) (fr-FR)
'    
'    F2 format string:   (13.47, -18.12) (en-US)    (13,47, -18,12) (fr-FR)
'    N2 format string:   (13.47, -18.12) (en-US)    (13,47, -18,12) (fr-FR)
'    G5 format string:   (13.472, -18.115) (en-US)    (13,472, -18,115) (fr-FR)
'    
'    F2 format string:   (-11.15, -17.00) (en-US)    (-11,15, -17,00) (fr-FR)
'    N2 format string:   (-11.15, -17.00) (en-US)    (-11,15, -17,00) (fr-FR)
'    G5 format string:   (-11.154, -17.002) (en-US)    (-11,154, -17,002) (fr-FR)

備註

這個方法傳回之複數的字串表示會以 <;b>的形式顯示數位,其中 是複數的實際部分,b 是其虛數部分。 b 都會使用 format所指定的格式字串來格式化。 format 參數可以是任何有效的標準數值格式規範,或自定義數值格式規範的任何組合。 如果 format 等於 String.Emptynull,則複數的實數和虛數部分會以一般格式規範(“G”) 格式化。 如果 format 為任何其他值,方法會擲回 FormatException

.NET Framework 提供廣泛的格式支援,在下列主題中會更詳細地說明:

provider 參數是 IFormatProvider 實作。 其 GetFormat 方法會傳回 NumberFormatInfo 物件,提供傳回字串中實數和虛數格式的文化特性特定資訊。 視 format 參數而定,此物件會控制輸出字串中的負號、群組分隔符和小數點符號等符號。 如果 providernull,則傳回的字串會使用目前文化特性的 NumberFormatInfo 物件來格式化。

provider 參數可以是下列其中一項:

另請參閱

適用於

ToString(String)

來源:
Complex.cs
來源:
Complex.cs
來源:
Complex.cs

使用指定的格式,將目前複數的值,轉換為其實數和虛數部分的相等字串表示。

public:
 System::String ^ ToString(System::String ^ format);
public string ToString (string format);
public string ToString (string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String

參數

format
String

標準或自定義數值格式字串。

傳回

以笛卡兒形式表示目前實例的字串表示。

例外狀況

format 不是有效的格式字串。

範例

下列範例會初始化複數,並使用數個標準格式字串加以顯示。

using System;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      Complex[] c = { new Complex(17.3, 14.1),
                      new Complex(-18.9, 147.2),
                      new Complex(13.472, -18.115),
                      new Complex(-11.154, -17.002) };
      string[] formats = { "F2", "N2", "G5" };

      foreach (Complex c1 in c)
      {
         foreach (string format in formats)
            Console.WriteLine("{0}: {1}    ", format, c1.ToString(format));

         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//       F2: (17.30, 14.10)
//       N2: (17.30, 14.10)
//       G5: (17.3, 14.1)
//
//       F2: (-18.90, 147.20)
//       N2: (-18.90, 147.20)
//       G5: (-18.9, 147.2)
//
//       F2: (13.47, -18.12)
//       N2: (13.47, -18.12)
//       G5: (13.472, -18.115)
//
//       F2: (-11.15, -17.00)
//       N2: (-11.15, -17.00)
//       G5: (-11.154, -17.002)
open System.Numerics

let c =
    [ Complex(17.3, 14.1)
      Complex(-18.9, 147.2)
      Complex(13.472, -18.115)
      Complex(-11.154, -17.002) ]

let formats = [ "F2"; "N2"; "G5" ]

for c1 in c do
    for format in formats do
        printf $"{format}: {c1.ToString(format)}    "

    printfn ""
// The example displays the following output:
//       F2: (17.30, 14.10)
//       N2: (17.30, 14.10)
//       G5: (17.3, 14.1)
//
//       F2: (-18.90, 147.20)
//       N2: (-18.90, 147.20)
//       G5: (-18.9, 147.2)
//
//       F2: (13.47, -18.12)
//       N2: (13.47, -18.12)
//       G5: (13.472, -18.115)
//
//       F2: (-11.15, -17.00)
//       N2: (-11.15, -17.00)
//       G5: (-11.154, -17.002)
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim c() As Complex = { New Complex(17.3, 14.1), 
                             New Complex(-18.9, 147.2), 
                             New Complex(13.472, -18.115), 
                             New Complex(-11.154, -17.002) }
      Dim formats() As String = { "F2", "N2", "G5" } 
      
      For Each c1 As Complex In c
         For Each format As String In formats
            Console.WriteLine("{0}: {1}    ", format, c1.ToString(format))
         Next
         Console.WriteLine()
      Next                          
   End Sub
End Module
' The example displays the following output:
'       F2: (17.30, 14.10)
'       N2: (17.30, 14.10)
'       G5: (17.3, 14.1)
'       
'       F2: (-18.90, 147.20)
'       N2: (-18.90, 147.20)
'       G5: (-18.9, 147.2)
'       
'       F2: (13.47, -18.12)
'       N2: (13.47, -18.12)
'       G5: (13.472, -18.115)
'       
'       F2: (-11.15, -17.00)
'       N2: (-11.15, -17.00)
'       G5: (-11.154, -17.002)

備註

這個方法傳回之複數的字串表示會以 <;b>的形式顯示數位,其中 是複數的實際部分,b 是其虛數部分。 b 都會使用 format所指定的格式字串來格式化。 format 參數可以是任何有效的標準數值格式規範,或自定義數值格式規範的任何組合。 如果 format 等於 String.Emptynull,則複數的實數和虛數部分會以一般格式規範(“G”) 格式化。 如果 format 為任何其他值,方法會擲回 FormatException

.NET Framework 提供廣泛的格式支援,在下列主題中會更詳細地說明:

傳回字串的格式是由目前文化特性的 NumberFormatInfo 對象所決定。 視 format 參數而定,此物件會控制輸出字串中的負號、群組分隔符和小數點符號等符號。 若要提供目前文化特性以外的文化特性的格式資訊,請呼叫 ToString(String, IFormatProvider) 多載。

另請參閱

適用於

ToString(IFormatProvider)

來源:
Complex.cs
來源:
Complex.cs
來源:
Complex.cs

使用指定的特定文化特性格式資訊,將目前複數的值轉換為其相等字串表示法。

public:
 System::String ^ ToString(IFormatProvider ^ provider);
public string ToString (IFormatProvider provider);
public string ToString (IFormatProvider? provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String

參數

provider
IFormatProvider

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

傳回

以笛卡兒形式表示目前實例的字串表示,如 provider所指定。

範例

下列範例會顯示數個複數的字串表示。 結果會使用英文 - 美國 (“en-US”) 和法文 - 法國 (“fr-FR” ) 文化特性的格式慣例。

using System;
using System.Globalization;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      Complex[] c = { new Complex(17.3, 14.1),
                      new Complex(-18.9, 147.2),
                      new Complex(13.472, -18.115),
                      new Complex(-11.154, -17.002) };
      CultureInfo[] cultures = { new CultureInfo("en-US"),
                                 new CultureInfo("fr-FR") };
      foreach (Complex c1 in c)
      {
         foreach (CultureInfo culture in cultures)
            Console.Write("{0} ({1})    ", c1.ToString(culture), culture.Name);

         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//       (17.3, 14.1) (en-US)    (17,3, 14,1) (fr-FR)
//       (-18.9, 147.2) (en-US)    (-18,9, 147,2) (fr-FR)
//       (13.472, -18.115) (en-US)    (13,472, -18,115) (fr-FR)
//       (-11.154, -17.002) (en-US)    (-11,154, -17,002) (fr-FR)
open System.Globalization
open System.Numerics

let c =
    [ Complex(17.3, 14.1)
      Complex(-18.9, 147.2)
      Complex(13.472, -18.115)
      Complex(-11.154, -17.002) ]

let cultures = [ CultureInfo "en-US"; CultureInfo "fr-FR" ]

for c1 in c do
    for culture in cultures do
        printf $"{c1.ToString culture} ({culture.Name})"

    printfn ""
// The example displays the following output:
//       (17.3, 14.1) (en-US)    (17,3, 14,1) (fr-FR)
//       (-18.9, 147.2) (en-US)    (-18,9, 147,2) (fr-FR)
//       (13.472, -18.115) (en-US)    (13,472, -18,115) (fr-FR)
//       (-11.154, -17.002) (en-US)    (-11,154, -17,002) (fr-FR)
Imports System.Globalization
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim c() As Complex = { New Complex(17.3, 14.1), 
                             New Complex(-18.9, 147.2), 
                             New Complex(13.472, -18.115), 
                             New Complex(-11.154, -17.002) }
      Dim cultures() As CultureInfo = { New CultureInfo("en-US"), 
                                        New CultureInfo("fr-FR") } 
      For Each c1 As Complex In c
         For Each culture As CultureInfo In cultures
            Console.Write("{0} ({1})    ", c1.ToString(culture), culture.Name)
         Next
         Console.WriteLine()
      Next                          
   End Sub
End Module
' The example displays the following output:
'       (17.3, 14.1) (en-US)    (17,3, 14,1) (fr-FR)
'       (-18.9, 147.2) (en-US)    (-18,9, 147,2) (fr-FR)
'       (13.472, -18.115) (en-US)    (13,472, -18,115) (fr-FR)
'       (-11.154, -17.002) (en-US)    (-11,154, -17,002) (fr-FR)

備註

這個方法傳回之複數的字串表示會以 <;b>的形式顯示數位,其中 是複數的實際部分,b 是其虛數部分。 b 都是使用一般格式規範 (“G”) 和 provider所定義之文化特性的慣例來格式化。

provider 參數是 IFormatProvider 實作。 其 GetFormat 方法會傳回 NumberFormatInfo 物件,提供傳回字串中實數和虛數格式的文化特性特定資訊。 如果 providernull,則傳回的字串會使用目前文化特性的 NumberFormatInfo 物件來格式化。

provider 參數可以是下列其中一項:

適用於

ToString()

來源:
Complex.cs
來源:
Complex.cs
來源:
Complex.cs

將目前複數的值轉換為以笛卡兒形式表示的對等字串表示。

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

以笛卡兒形式表示目前實例的字串表示。

範例

下列範例會顯示數個複數的字串表示。 輸出會使用英文 - 美國 (“en-US”) 文化特性的格式慣例,在此案例中為目前的系統文化特性。

using System;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      Complex[] c = { new Complex(17.3, 14.1),
                      new Complex(-18.9, 147.2),
                      new Complex(13.472, -18.115),
                      new Complex(-11.154, -17.002) };
      foreach (Complex c1 in c)
         Console.WriteLine(c1.ToString());
   }
}
// The example display the following output:
//       (17.3, 14.1)
//       (-18.9, 147.2)
//       (13.472, -18.115)
//       (-11.154, -17.002)
open System.Numerics

let c =
    [ Complex(17.3, 14.1)
      Complex(-18.9, 147.2)
      Complex(13.472, -18.115)
      Complex(-11.154, -17.002) ]

for c1 in c do
    printfn $"{c1.ToString()}"
// The example display the following output:
//       (17.3, 14.1)
//       (-18.9, 147.2)
//       (13.472, -18.115)
//       (-11.154, -17.002)
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim c() As Complex = { New Complex(17.3, 14.1), 
                             New Complex(-18.9, 147.2), 
                             New Complex(13.472, -18.115), 
                             New Complex(-11.154, -17.002) }
      For Each c1 As Complex In c
         Console.WriteLine(c1.ToString())
      Next                          
   End Sub
End Module
' The example displays the following output:
'       (17.3, 14.1)
'       (-18.9, 147.2)
'       (13.472, -18.115)
'       (-11.154, -17.002)

備註

複數的預設字串表示會以 <;b>的形式顯示數位,其中 是複數的實際部分,而 b 是其虛數部分。 b 都會使用一般格式規範(“G”) 和目前系統文化特性的慣例來格式化。

適用於