Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tracked by #79004
fryguy1013 opened this issue Jun 8, 2022 · 7 comments
Assignees
Labels
area-System.Numerics bug help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@fryguy1013
Copy link

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 #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

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jun 8, 2022
@ghost
Copy link

ghost commented Jun 8, 2022

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Issue Details

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 #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

Author: fryguy1013
Assignees: -
Labels:

area-System.Numerics

Milestone: -

@danmoseley
Copy link
Member

This was "+0.00" in .NET Core 2.1 and changed to "-+0.00" in .NET Core 3.1

@tannergooding
Copy link
Member

I'm guessing there is a conflict between the input value and the rounded value. Will take a look.

@jwosty
Copy link

jwosty commented Jul 5, 2023

Depending on your use case, it's possible to work around it using all three section separators, but you lose the ability to print negative and positive zero differently.

For example:

(-0.0).ToString("+0.0;-0.0;0.0")

and

(-0.0000001).ToString("+0.0;-0.0;0.0")

both display 0.0.

@fryguy1013
Copy link
Author

The workaround we're using to preserve the behavior is in the issue. Your idea will display "0.0" instead of "+0.0" (or "-0.0") which isn't the right size since the protocol requires a sign bit in that position of the string.

@jwosty
Copy link

jwosty commented Jul 5, 2023

The workaround we're using to preserve the behavior is in the issue. Your idea will display "0.0" instead of "+0.0" (or "-0.0") which isn't the right size since the protocol requires a sign bit in that position of the string.

Ah, I see I didn't read that properly (it was late at night :) ). You're right, that is the better workaround. Any of these workaround using the third section still have the problem of not preserving the difference between -0.0 and +0.0 though -- therefore there is no real workaround if you really need that behavior (other than writing your own formatting code). A true fix would be great.

@tannergooding tannergooding modified the milestones: 8.0.0, 9.0.0, Future Aug 14, 2023
@tannergooding tannergooding added the help wanted [up-for-grabs] Good issue for external contributors label Aug 14, 2023
@tannergooding
Copy link
Member

This is effectively a do anytime issue. The bug is small and shouldn't be overly difficult to fix. For the most part -0.0 needs to be treated as negative and distinct from +0.0 for the purposes of formatting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Numerics bug help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants