Skip to content

Commit c0a7d49

Browse files
authored
Fix output of complex number formatting (#46235)
* fix output of complex number formatting * break long lines
1 parent 8a297d1 commit c0a7d49

File tree

14 files changed

+54
-47
lines changed

14 files changed

+54
-47
lines changed

docs/core/compatibility/core-libraries/8.0/complex-format.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ If you need the previous format, you can use a custom string formatting mechanis
3636
## Affected APIs
3737

3838
- <xref:System.Numerics.Complex.ToString%2A?displayProperty=fullName>
39+
40+
## See also
41+
42+
- [Format a complex number](../../../../fundamentals/runtime-libraries/system-numerics-complex.md#format-a-complex-number)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CustomFormatEx.Run();
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>Library</OutputType>
5-
<TargetFrameworks>net8</TargetFrameworks>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net9</TargetFrameworks>
66
</PropertyGroup>
77

88
</Project>

docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/create1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class CreateEx
66
{
7-
public static void Main()
7+
public static void Run()
88
{
99
// Create a complex number by calling its class constructor.
1010
Complex c1 = new Complex(12, 6);

docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/customfmt1.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@ public object GetFormat(Type formatType)
1515
public string Format(string format, object arg,
1616
IFormatProvider provider)
1717
{
18-
if (arg is Complex)
18+
if (arg is Complex c1)
1919
{
20-
Complex c1 = (Complex)arg;
2120
// Check if the format string has a precision specifier.
2221
int precision;
23-
string fmtString = String.Empty;
22+
string fmtString = string.Empty;
2423
if (format.Length > 1)
2524
{
2625
try
2726
{
28-
precision = Int32.Parse(format.Substring(1));
27+
precision = int.Parse(format.Substring(1));
2928
}
3029
catch (FormatException)
3130
{
@@ -42,12 +41,12 @@ public string Format(string format, object arg,
4241
}
4342
else
4443
{
45-
if (arg is IFormattable)
46-
return ((IFormattable)arg).ToString(format, provider);
44+
if (arg is IFormattable formattable)
45+
return formattable.ToString(format, provider);
4746
else if (arg != null)
4847
return arg.ToString();
4948
else
50-
return String.Empty;
49+
return string.Empty;
5150
}
5251
}
5352
}
@@ -56,22 +55,21 @@ public string Format(string format, object arg,
5655
// <Snippet4>
5756
public class CustomFormatEx
5857
{
59-
public static void Main()
58+
public static void Run()
6059
{
61-
Complex c1 = new Complex(12.1, 15.4);
62-
Console.WriteLine("Formatting with ToString(): " +
63-
c1.ToString());
64-
Console.WriteLine("Formatting with ToString(format): " +
65-
c1.ToString("N2"));
66-
Console.WriteLine("Custom formatting with I0: " +
67-
String.Format(new ComplexFormatter(), "{0:I0}", c1));
68-
Console.WriteLine("Custom formatting with J3: " +
69-
String.Format(new ComplexFormatter(), "{0:J3}", c1));
60+
Complex c1 = new(12.1, 15.4);
61+
Console.WriteLine($"Formatting with ToString: {c1}");
62+
Console.WriteLine($"Formatting with ToString(format): {c1:N2}");
63+
Console.WriteLine($"Custom formatting with I0:\t" +
64+
$" {string.Format(new ComplexFormatter(), "{0:I0}", c1)}");
65+
Console.WriteLine($"Custom formatting with J3:\t" +
66+
$" {string.Format(new ComplexFormatter(), "{0:J3}", c1)}");
7067
}
7168
}
69+
7270
// The example displays the following output:
73-
// Formatting with ToString(): (12.1, 15.4)
74-
// Formatting with ToString(format): (12.10, 15.40)
71+
// Formatting with ToString(): <12.1; 15.4>
72+
// Formatting with ToString(format): <12.10; 15.40>
7573
// Custom formatting with I0: 12 + 15i
7674
// Custom formatting with J3: 12.100 + 15.400j
7775
// </Snippet4>

docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/nan1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class NaNEx
66
{
7-
public static void Main()
7+
public static void Run()
88
{
99
Complex c1 = new Complex(Double.MaxValue / 2, Double.MaxValue / 2);
1010

docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/precision1.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System;
1+
using System;
22
using System.Numerics;
33

44
public class PrecisionEx
55
{
6-
public static void Main()
6+
public static void Run()
77
{
88
// <Snippet5>
99
Complex value = new Complex(Double.MinValue / 2, Double.MinValue / 2);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Public Class Program
2+
Public Shared Sub Main()
3+
Example2.Run()
4+
End Sub
5+
End Class
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>Library</OutputType>
5-
<TargetFrameworks>net8</TargetFrameworks>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net9</TargetFrameworks>
66
</PropertyGroup>
77

88
</Project>

docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/vb/create1.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Option Strict On
55
Imports System.Numerics
66

77
Module Example
8-
Public Sub Main()
8+
Public Sub Run()
99
' Create a complex number by calling its class constructor.
1010
Dim c1 As New Complex(12, 6)
1111
Console.WriteLine(c1)

0 commit comments

Comments
 (0)