Complex.ToString 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將複數的值轉換為其相等的字串表示。
多載
| 名稱 | Description |
|---|---|
| ToString(String, IFormatProvider) |
使用指定的格式和文化特性特定格式資訊,將目前複數的值轉換為其以笛卡兒形式表示的對等字串表示法,以實數和虛數部分。 |
| ToString(String) |
使用指定的格式,將目前複數的值,轉換為其實數和虛數部分的相等字串表示。 |
| ToString(IFormatProvider) |
使用指定的特定文化特性格式資訊,將目前複數的值轉換為其相等字串表示法。 |
| ToString() |
將目前複數的值轉換為以笛卡兒形式表示的對等字串表示。 |
ToString(String, IFormatProvider)
- 來源:
- Complex.cs
- 來源:
- Complex.cs
- 來源:
- 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
物件,提供特定文化特性的格式資訊。
傳回
目前實例以笛卡兒形式表示的弦表示,由 和 provider指定format。
實作
例外狀況
format 不是一個有效的格式字串。
範例
以下範例建立複數陣列,並使用多個標準格式字串 CultureInfo 以及代表英語-美國(「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") };
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)
備註
此方法回傳的複數字串表示法是以笛卡兒座標 <a; b> 形式顯示該數(或 (a, b) 在 .NET Framework 中),其中 a 是複數的實部, b 是其虛部。
a 和 b 皆使用由 format所指定的格式字串格式化。 參數 format 可以是任何有效的標準數值格式規範符,或是任意自訂數值格式規範符的組合。 若 format 等於或 String.Empty 為 null,複數的實部與虛部則以一般格式指定符(“G”)格式化。 如果 format 為任何其他值,方法會擲回 FormatException。
.NET 提供廣泛的格式支援,如下列文章更詳細地說明:
參數 provider 是一個 IFormatProvider 實作。 其 GetFormat 方法回傳一個 NumberFormatInfo 物件,提供關於回傳字串中實數與虛數格式的文化特定資訊。 根據參數 format 不同,此物件控制負符號、群分隔符及輸出字串中的小數點符號等符號。 若 provider 為 null,則回傳字串會使用 NumberFormatInfo 當前文化的物件進行格式化。
參數 provider 可以是以下之一:
- 一個 CultureInfo 代表文化的物件,提供格式資訊
- 提供 NumberFormatInfo 格式資訊的物件。
- 實作 IFormatProvider 介面的自定義物件。 其 GetFormat 方法回傳 NumberFormatInfo 提供格式資訊的物件。
另請參閱
適用於
ToString(String)
- 來源:
- Complex.cs
- 來源:
- Complex.cs
- 來源:
- 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)
備註
此方法回傳的複數字串表示法是以笛卡兒座標 <a; b> 形式顯示該數(或 (a, b) 在 .NET Framework 中),其中 a 是複數的實部, b 是其虛部。
a 和 b 皆使用由 format所指定的格式字串格式化。 參數 format 可以是任何有效的標準數值格式規範符,或是任意自訂數值格式規範符的組合。 若 format 等於或 String.Empty 為 null,複數的實部與虛部則以一般格式指定符(“G”)格式化。 如果 format 為任何其他值,方法會擲回 FormatException。
.NET 提供廣泛的格式支援,如下列文章更詳細地說明:
回傳字串的格式由 NumberFormatInfo 當前文化的物件決定。 根據參數 format 不同,此物件控制負符號、群分隔符及輸出字串中的小數點符號等符號。 若要提供非當前文化的格式資訊,請呼叫 ToString(String, IFormatProvider) overload。
另請參閱
適用於
ToString(IFormatProvider)
- 來源:
- Complex.cs
- 來源:
- Complex.cs
- 來源:
- 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)
備註
此方法回傳的複數字串表示法是以笛卡兒座標 <a; b> 形式顯示該數(或 (a, b) 在 .NET Framework 中),其中 a 是複數的實部, b 是其虛部。
a 和 b 皆使用一般格式指定符(“G”)及由 provider定義的文化慣例來格式化。
參數 provider 是一個 IFormatProvider 實作。 其 GetFormat 方法回傳一個 NumberFormatInfo 物件,提供關於回傳字串中實數與虛數格式的文化特定資訊。 若 provider 為 null,則回傳字串會使用 NumberFormatInfo 當前文化的物件進行格式化。
參數 provider 可以是以下之一:
- 一個 CultureInfo 代表文化的物件,提供格式資訊
- 提供 NumberFormatInfo 格式資訊的物件。
- 實作 IFormatProvider 介面的自定義物件。 其 GetFormat 方法回傳 NumberFormatInfo 提供格式資訊的物件。
適用於
ToString()
- 來源:
- Complex.cs
- 來源:
- Complex.cs
- 來源:
- 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)
備註
複數的預設字串表示法是以笛卡兒座標 <a; b> 的形式(或 (a, b) 在 .NET Framework 中)顯示該數字, 其中 a 是複數的實部, b 是其虛部。
a 與 b 皆採用一般格式指定符(「G」)及當前系統文化的慣例來格式化。