Skip to content

Commit 0c43c35

Browse files
authored
Fix Complex number custom formatting for negative imaginary parts (#46897)
1 parent 208c82a commit 0c43c35

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

docs/fundamentals/runtime-libraries/snippets/System.Numerics/Complex/Overview/csharp/Project.csproj

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

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net9</TargetFrameworks>
5+
<TargetFramework>net9.0</TargetFramework>
66
</PropertyGroup>
77

88
</Project>

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,19 @@ public string Format(string format, object arg,
3333
fmtString = "N" + precision.ToString();
3434
}
3535
if (format.Substring(0, 1).Equals("I", StringComparison.OrdinalIgnoreCase))
36-
return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "i";
36+
{
37+
// Determine the sign to display.
38+
char sign = c1.Imaginary < 0 ? '-' : '+';
39+
// Display the determined sign and the absolute value of the imaginary part.
40+
return c1.Real.ToString(fmtString) + " " + sign + " " + Math.Abs(c1.Imaginary).ToString(fmtString) + "i";
41+
}
3742
else if (format.Substring(0, 1).Equals("J", StringComparison.OrdinalIgnoreCase))
38-
return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "j";
43+
{
44+
// Determine the sign to display.
45+
char sign = c1.Imaginary < 0 ? '-' : '+';
46+
// Display the determined sign and the absolute value of the imaginary part.
47+
return c1.Real.ToString(fmtString) + " " + sign + " " + Math.Abs(c1.Imaginary).ToString(fmtString) + "j";
48+
}
3949
else
4050
return c1.ToString(format, provider);
4151
}

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

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

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net9</TargetFrameworks>
5+
<TargetFramework>net9.0</TargetFramework>
66
</PropertyGroup>
77

88
</Project>

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ Public Class ComplexFormatter
3232
End Try
3333
fmtString = "N" + precision.ToString()
3434
End If
35+
' Determine the sign to display.
36+
Dim sign As Char = If(c1.Imaginary < 0.0, "-"c, "+"c)
37+
' Display the determined sign and the absolute value of the imaginary part.
3538
If fmt.Substring(0, 1).Equals("I", StringComparison.OrdinalIgnoreCase) Then
36-
Return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "i"
39+
Return c1.Real.ToString(fmtString) + " " + sign + " " + Math.Abs(c1.Imaginary).ToString(fmtString) + "i"
3740
ElseIf fmt.Substring(0, 1).Equals("J", StringComparison.OrdinalIgnoreCase) Then
38-
Return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "j"
41+
Return c1.Real.ToString(fmtString) + " " + sign + " " + Math.Abs(c1.Imaginary).ToString(fmtString) + "j"
3942
Else
4043
Return c1.ToString(fmt, provider)
4144
End If

0 commit comments

Comments
 (0)