Complex.ToString 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将复数的值转换为其等效的字符串表示形式。
重载
ToString(String, IFormatProvider) |
通过对当前复数的实部和虚部使用指定格式和区域性特定格式信息,将它的值转换为其采用笛卡尔形式的等效字符串表示形式。 |
ToString(String) |
通过对当前复数的实部和虚部使用指定格式,将它的值转换为其采用笛卡尔形式的等效字符串表示形式。 |
ToString(IFormatProvider) |
使用指定的区域性特定格式设置信息,将当前复数的值转换为其采用笛卡尔形式的等效字符串表示形式。 |
ToString() |
将当前复数的值转换为其采用笛卡尔形式的等效字符串表示形式。 |
ToString(String, IFormatProvider)
- Source:
- Complex.cs
- Source:
- Complex.cs
- Source:
- 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
一个提供区域性特定的格式设置信息的对象。
返回
由 format
和 provider
指定的当前实例的采用笛卡尔形式的字符串表示形式。
实现
例外
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)
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)
的形式,
(
显示其笛卡尔坐标的数字,其中 a 是复数的实际部分,b 是其虚部分。 使用 指定的format
格式字符串设置 a 和 b 的格式。 参数 format
可以是任何有效的标准数字格式说明符,也可以是自定义数值格式说明符的任意组合。 如果 format
等于 String.Empty 或 为 null
,则复数的实部分和虚部分使用常规格式说明符 (“G”) 进行格式化。 如果 format
是任何其他值,则 方法将 FormatException引发 。
.NET Framework提供了广泛的格式设置支持,以下主题对此进行了更详细的介绍:
有关数字格式字符串的详细信息,请参阅 标准数字格式字符串 和 自定义数值格式字符串。
有关.NET Framework中格式设置的详细信息,请参阅格式设置类型。
参数 provider
是实现 IFormatProvider 。 其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关返回的字符串中实数和虚数的格式的区域性特定信息。 根据 format
参数,此对象控制输出字符串中的负号、组分隔符和小数点符号等符号。 如果 provider
为 null
,则使用 NumberFormatInfo 当前区域性的 对象设置返回的字符串的格式。
参数 provider
可以是以下参数之一:
一个 CultureInfo 对象,该对象表示提供格式设置信息的区域性
NumberFormatInfo提供格式设置信息的 对象。
实现 接口的 IFormatProvider 自定义对象。 其 GetFormat 方法返回 NumberFormatInfo 提供格式设置信息的 对象。
另请参阅
适用于
ToString(String)
- Source:
- Complex.cs
- Source:
- Complex.cs
- Source:
- 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)
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)
的形式,
(
显示其笛卡尔坐标的数字,其中 a 是复数的实际部分,b 是其虚部分。 使用 指定的format
格式字符串设置 a 和 b 的格式。 参数 format
可以是任何有效的标准数字格式说明符,也可以是自定义数值格式说明符的任意组合。 如果 format
等于 String.Empty 或 为 null
,则复数的实部分和虚部分使用常规格式说明符 (“G”) 进行格式化。 如果 format
是任何其他值,则 方法将 FormatException引发 。
.NET Framework提供了广泛的格式设置支持,以下主题对此进行了更详细的介绍:
有关数字格式字符串的详细信息,请参阅 标准数字格式字符串 和 自定义数值格式字符串。
有关.NET Framework中格式设置的详细信息,请参阅格式设置类型。
返回的字符串的格式由 NumberFormatInfo 当前区域性的 对象确定。 根据 format
参数,此对象控制输出字符串中的负号、组分隔符和小数点符号等符号。 若要为当前区域性以外的区域性提供格式设置信息,请调用 ToString(String, IFormatProvider) 重载。
另请参阅
适用于
ToString(IFormatProvider)
- Source:
- Complex.cs
- Source:
- Complex.cs
- Source:
- 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)
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)
的形式,
(
显示其笛卡尔坐标的数字,其中 a 是复数的实际部分,b 是其虚部分。 使用常规格式说明符 (“G”) 以及 定义的provider
区域性约定设置 a 和 b 的格式。
参数 provider
是实现 IFormatProvider 。 其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关返回的字符串中实数和虚数的格式的区域性特定信息。 如果 provider
为 null
,则使用 NumberFormatInfo 当前区域性的 对象设置返回的字符串的格式。
参数 provider
可以是以下参数之一:
一个 CultureInfo 对象,该对象表示提供格式设置信息的区域性
NumberFormatInfo提供格式设置信息的 对象。
实现 接口的 IFormatProvider 自定义对象。 其 GetFormat 方法返回 NumberFormatInfo 提供格式设置信息的 对象。
适用于
ToString()
- Source:
- Complex.cs
- Source:
- Complex.cs
- Source:
- 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)
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)
的形式,
(
使用其笛卡尔坐标显示数字,其中 a 是复数的实际部分,b 是其虚部分。 a 和 b 都使用常规格式说明符 (“G”) 和当前系统区域性的约定进行格式化。
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈