diff --git a/docs/core/compatibility/core-libraries/8.0/complex-format.md b/docs/core/compatibility/core-libraries/8.0/complex-format.md index 88bfbe2a83905..d6f471d87673c 100644 --- a/docs/core/compatibility/core-libraries/8.0/complex-format.md +++ b/docs/core/compatibility/core-libraries/8.0/complex-format.md @@ -36,3 +36,7 @@ If you need the previous format, you can use a custom string formatting mechanis ## Affected APIs - + +## See also + +- [Format a complex number](../../../../fundamentals/runtime-libraries/system-numerics-complex.md#format-a-complex-number) diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/Program.cs b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/Program.cs new file mode 100644 index 0000000000000..99e6662d51bc0 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/Program.cs @@ -0,0 +1 @@ +CustomFormatEx.Run(); diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/Project.csproj b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/Project.csproj index 251a8cb33ffed..5ed8f36be8cd5 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/Project.csproj +++ b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/Project.csproj @@ -1,8 +1,8 @@ - Library - net8 + Exe + net9 diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/create1.cs b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/create1.cs index 6ee26e1f5d796..10d20d59111f9 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/create1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/create1.cs @@ -4,7 +4,7 @@ public class CreateEx { - public static void Main() + public static void Run() { // Create a complex number by calling its class constructor. Complex c1 = new Complex(12, 6); diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/customfmt1.cs b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/customfmt1.cs index 3229bb7cf2dc7..496a4a4e92f6f 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/customfmt1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/customfmt1.cs @@ -15,17 +15,16 @@ public object GetFormat(Type formatType) public string Format(string format, object arg, IFormatProvider provider) { - if (arg is Complex) + if (arg is Complex c1) { - Complex c1 = (Complex)arg; // Check if the format string has a precision specifier. int precision; - string fmtString = String.Empty; + string fmtString = string.Empty; if (format.Length > 1) { try { - precision = Int32.Parse(format.Substring(1)); + precision = int.Parse(format.Substring(1)); } catch (FormatException) { @@ -42,12 +41,12 @@ public string Format(string format, object arg, } else { - if (arg is IFormattable) - return ((IFormattable)arg).ToString(format, provider); + if (arg is IFormattable formattable) + return formattable.ToString(format, provider); else if (arg != null) return arg.ToString(); else - return String.Empty; + return string.Empty; } } } @@ -56,22 +55,21 @@ public string Format(string format, object arg, // public class CustomFormatEx { - public static void Main() + public static void Run() { - Complex c1 = new Complex(12.1, 15.4); - Console.WriteLine("Formatting with ToString(): " + - c1.ToString()); - Console.WriteLine("Formatting with ToString(format): " + - c1.ToString("N2")); - Console.WriteLine("Custom formatting with I0: " + - String.Format(new ComplexFormatter(), "{0:I0}", c1)); - Console.WriteLine("Custom formatting with J3: " + - String.Format(new ComplexFormatter(), "{0:J3}", c1)); + Complex c1 = new(12.1, 15.4); + Console.WriteLine($"Formatting with ToString: {c1}"); + Console.WriteLine($"Formatting with ToString(format): {c1:N2}"); + Console.WriteLine($"Custom formatting with I0:\t" + + $" {string.Format(new ComplexFormatter(), "{0:I0}", c1)}"); + Console.WriteLine($"Custom formatting with J3:\t" + + $" {string.Format(new ComplexFormatter(), "{0:J3}", c1)}"); } } + // The example displays the following output: -// Formatting with ToString(): (12.1, 15.4) -// Formatting with ToString(format): (12.10, 15.40) +// Formatting with ToString(): <12.1; 15.4> +// Formatting with ToString(format): <12.10; 15.40> // Custom formatting with I0: 12 + 15i // Custom formatting with J3: 12.100 + 15.400j // diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/nan1.cs b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/nan1.cs index ae5c738d3eeca..c91485224155b 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/nan1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/nan1.cs @@ -4,7 +4,7 @@ public class NaNEx { - public static void Main() + public static void Run() { Complex c1 = new Complex(Double.MaxValue / 2, Double.MaxValue / 2); diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/precision1.cs b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/precision1.cs index 47626a472219c..c2e851c3b5a49 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/precision1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/precision1.cs @@ -1,9 +1,9 @@ -using System; +using System; using System.Numerics; public class PrecisionEx { - public static void Main() + public static void Run() { // Complex value = new Complex(Double.MinValue / 2, Double.MinValue / 2); diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/Program.vb b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/Program.vb new file mode 100644 index 0000000000000..8cd0a0ebcc1ee --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/Program.vb @@ -0,0 +1,5 @@ +Public Class Program + Public Shared Sub Main() + Example2.Run() + End Sub +End Class diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/Project.vbproj b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/Project.vbproj index 251a8cb33ffed..5ed8f36be8cd5 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/Project.vbproj +++ b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/Project.vbproj @@ -1,8 +1,8 @@ - Library - net8 + Exe + net9 diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/create1.vb b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/create1.vb index eb9e5a73bd268..fdfaf39259663 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/create1.vb +++ b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/create1.vb @@ -5,7 +5,7 @@ Option Strict On Imports System.Numerics Module Example - Public Sub Main() + Public Sub Run() ' Create a complex number by calling its class constructor. Dim c1 As New Complex(12, 6) Console.WriteLine(c1) diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/customfmt1.vb b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/customfmt1.vb index 7c197d17d8735..ee1efcfb2401b 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/customfmt1.vb +++ b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/customfmt1.vb @@ -26,7 +26,7 @@ Public Class ComplexFormatter Dim fmtString As String = String.Empty If fmt.Length > 1 Then Try - precision = Int32.Parse(fmt.Substring(1)) + precision = Integer.Parse(fmt.Substring(1)) Catch e As FormatException precision = 0 End Try @@ -54,21 +54,20 @@ End Class ' Module Example2 - Public Sub Main() - Dim c1 As Complex = New Complex(12.1, 15.4) - Console.WriteLine("Formatting with ToString(): " + - c1.ToString()) - Console.WriteLine("Formatting with ToString(format): " + - c1.ToString("N2")) - Console.WriteLine("Custom formatting with I0: " + - String.Format(New ComplexFormatter(), "{0:I0}", c1)) - Console.WriteLine("Custom formatting with J3: " + - String.Format(New ComplexFormatter(), "{0:J3}", c1)) + Public Sub Run() + Dim c1 As New Complex(12.1, 15.4) + Console.WriteLine($"Formatting with ToString(): {c1}") + Console.WriteLine($"Formatting with ToString(format): {c1:N2}") + Console.WriteLine($"Custom formatting with I0: " + + $"{String.Format(New ComplexFormatter(), "{0:I0}", c1)}") + Console.WriteLine($"Custom formatting with J3: " + + $"{String.Format(New ComplexFormatter(), "{0:J3}", c1)}") End Sub End Module + ' The example displays the following output: -' Formatting with ToString(): (12.1, 15.4) -' Formatting with ToString(format): (12.10, 15.40) +' Formatting with ToString(): <12.1; 15.4> +' Formatting with ToString(format): <12.10; 15.40> ' Custom formatting with I0: 12 + 15i ' Custom formatting with J3: 12.100 + 15.400j ' diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/nan1.vb b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/nan1.vb index 8d178915a2abd..b794990719e2c 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/nan1.vb +++ b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/nan1.vb @@ -5,7 +5,7 @@ Option Strict On Imports System.Numerics Module Example4 - Public Sub Main() + Public Sub Run() Dim c1 As Complex = New Complex(Double.MaxValue / 2, Double.MaxValue / 2) Dim c2 As Complex = c1 / Complex.Zero diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/precision1.vb b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/precision1.vb index f677a474ff0da..32247a08f2201 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/precision1.vb +++ b/docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/precision1.vb @@ -4,7 +4,7 @@ Option Strict On Imports System.Numerics Module Example3 - Public Sub Main() + Public Sub Run() ' Dim value As New Complex(Double.MinValue / 2, Double.MinValue / 2) Dim value2 As Complex = Complex.Exp(Complex.Log(value)) diff --git a/docs/fundamentals/runtime-libraries/system-numerics-complex.md b/docs/fundamentals/runtime-libraries/system-numerics-complex.md index 3bb6bac6108b0..9724114c4c5fd 100644 --- a/docs/fundamentals/runtime-libraries/system-numerics-complex.md +++ b/docs/fundamentals/runtime-libraries/system-numerics-complex.md @@ -1,7 +1,7 @@ --- title: System.Numerics.Complex struct description: Learn about the System.Numerics.Complex struct. -ms.date: 12/31/2023 +ms.date: 05/15/2025 dev_langs: - CSharp - VB @@ -85,11 +85,11 @@ Note that this applies to any intermediate calculations performed by a method. F ## Format a complex number -By default, the string representation of a complex number takes the form `(`*real*`,` *imaginary*`)`, where *real* and *imaginary* are the string representations of the values that form the complex number's real and imaginary components. Some overloads of the method allow customization of the string representations of these values to reflect the formatting conventions of a particular culture or to appear in a particular format defined by a standard or custom numeric format string. (For more information, see [Standard Numeric Format Strings](../../standard/base-types/standard-numeric-format-strings.md) and [Custom Numeric Format Strings](../../standard/base-types/custom-numeric-format-strings.md).) +By default, the string representation of a complex number takes the form `<`*real*`;` *imaginary*`>`, where *real* and *imaginary* are the string representations of the values that form the complex number's real and imaginary components. Some overloads of the method allow customization of the string representations of these values to reflect the formatting conventions of a particular culture or to appear in a particular format defined by a standard or custom numeric format string. (For more information, see [Standard Numeric Format Strings](../../standard/base-types/standard-numeric-format-strings.md) and [Custom Numeric Format Strings](../../standard/base-types/custom-numeric-format-strings.md).) -One of the more common ways of expressing the string representation of a complex number takes the form a + bi, where a is the complex number's real component, and b is the complex number's imaginary component. In electrical engineering, a complex number is most commonly expressed as a + bj. You can return the string representation of a complex number in either of these two forms. To do this, define a custom format provider by implementing the and interfaces, and then call the method. +One of the more common ways of expressing the string representation of a complex number takes the form `a + bi`, where `a` is the complex number's real component, and `b` is the complex number's imaginary component. In electrical engineering, a complex number is most commonly expressed as `a + bj`. You can return the string representation of a complex number in either of these two forms. To do this, define a custom format provider by implementing the and interfaces, and then call the method. -The following example defines a `ComplexFormatter` class that represents a complex number as a string in the form of either a + bi or a + bj. +The following example defines a `ComplexFormatter` class that represents a complex number as a string in the form of either `a + bi` or `a + bj`. :::code language="csharp" source="./snippets/System.Numerics/Complex/Overview/csharp/customfmt1.cs" id="Snippet1"::: :::code language="vb" source="./snippets/System.Numerics/Complex/Overview/vb/customfmt1.vb" id="Snippet1":::