dotnet/runtime

Double.ToString with two section separators and negative zero placing additional negative sign in output

Open

#70.460 geöffnet am 8. Juni 2022

Auf GitHub ansehen
 (7 Kommentare) (2 Reaktionen) (1 zugewiesene Person)C# (5.445 Forks)batch import
area-System.Numericsbughelp wanted

Repository-Metriken

Stars
 (17.886 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 12T 11h) (661 gemergte PRs in 30 T)

Beschreibung

Description

When using a string format with two section separators and a negative zeroish number when accounting for the limited number of decimal points, there is an additional negative sign added to the output string when there shouldn't be.

Reproduction Steps

Console.WriteLine((-0.001).ToString("+0.00;-0.00")); ,

Expected behavior

Outputs +0.00 on .NET Framework 4.7.2 (-0.00 seems valid as well)

Actual behavior

outputs -+0.00 on dotnet6

Regression?

This worked in .NET Framework 4.7.2.

Known Workarounds

The two-section "+0.00;-0.00" can be replaced by the three-section "+0.00;-0.00;+0.00" and seems to work correctly

Configuration

I've reproduced this in both my local visual studio and dotnetfiddle.net on dotnet 6.

Other information

This seems related to the bug https://github.com/dotnet/runtime/issues/32237 which was an "Closed As Design" due to https://devblogs.microsoft.com/dotnet/floating-point-parsing-and-formatting-improvements-in-net-core-3-0/, but this issue seems like an actual regression since the string format is explicitly calling out how to format positive and negative numbers

Contributor Guide