-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Updating NumberToStringFormat to not print the sign if there are no digits being returned #20109
Updating NumberToStringFormat to not print the sign if there are no digits being returned #20109
Conversation
As an example: var nfi = new NumberFormatInfo();
nfi.NaNSymbol = "NaN";
nfi.PositiveSign = "+";
nfi.NegativeSign = "-";
nfi.PerMilleSymbol = "x";
nfi.PositiveInfinitySymbol = "Infinity";
nfi.NegativeInfinitySymbol = "-Infinity";
nfi.NumberDecimalDigits = 5;
nfi.NumberDecimalSeparator = ".";
nfi.NumberGroupSeparator = ",";
nfi.NumberGroupSizes = new int[] { 3 };
nfi.NumberNegativePattern = 2;
nfi.CurrencyDecimalDigits = 2;
nfi.CurrencyDecimalSeparator = ".";
nfi.CurrencyGroupSeparator = ",";
nfi.CurrencyGroupSizes = new int[] { 3 };
nfi.CurrencyNegativePattern = 8;
nfi.CurrencyPositivePattern = 3;
nfi.CurrencySymbol = "$";
nfi.PercentDecimalDigits = 5;
nfi.PercentDecimalSeparator = ".";
nfi.PercentGroupSeparator = ",";
nfi.PercentGroupSizes = new int[] { 3 };
nfi.PercentNegativePattern = 0;
nfi.PercentPositivePattern = 0;
nfi.PercentSymbol = "%";
var str = (-0.0).ToString(nfi);
Console.WriteLine($"'{str}'");
str = (-0.0).ToString("0", nfi);
Console.WriteLine($"'{str}'");
str = (-0.0).ToString("0,", nfi);
Console.WriteLine($"'{str}'");
str = (-0.0).ToString("0,,", nfi);
Console.WriteLine($"'{str}'");
str = (-0.0).ToString("#", nfi);
Console.WriteLine($"'{str}'");
str = (-0.0).ToString("#,", nfi);
Console.WriteLine($"'{str}'");
str = (-0.0).ToString("#,,", nfi);
Console.WriteLine($"'{str}'");
str = (-1234.0).ToString(nfi);
Console.WriteLine($"'{str}'");
str = (-1234.0).ToString("0", nfi);
Console.WriteLine($"'{str}'");
str = (-1234.0).ToString("0,", nfi);
Console.WriteLine($"'{str}'");
str = (-1234.0).ToString("0,,", nfi);
Console.WriteLine($"'{str}'");
str = (-1234.0).ToString("#", nfi);
Console.WriteLine($"'{str}'");
str = (-1234.0).ToString("#,", nfi);
Console.WriteLine($"'{str}'");
str = (-1234.0).ToString("#,,", nfi);
Console.WriteLine($"'{str}'"); Prints the following in 2.1:
and will now print:
|
@EgorBo pointed out the bug here: #20080 (comment) The issue was introduced in #19775, which was fixing up the formatter to properly print |
Working on adding some additional tests to CoreFX to cover this scenario. |
CC. @jkotas, @danmosemsft |
@@ -1929,6 +1927,9 @@ internal static unsafe void NumberToStringFormat(ref ValueStringBuilder sb, ref | |||
} | |||
} | |||
} | |||
|
|||
if (number.sign && (section == 0) && (sb.Length > 0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could keep the original path and have the code down here only for the case where scale == 0
.
For the scale == 0
case, I didn't see a clear way to check if a digit would be printed outside duplicating a number of the checks from the while loop.
Failing CoreFX test is with a custom format specifier Given the documentation on The ";" section separator, I wonder if the current behavior is a bug and the new behavior is correct.
I would think this means that the sign is meant to never be returned, including when there are no digits given. Edit: I also wouldn't expect it to have a different behavior from the single section case, given the other comments there. |
I agree that test sounds wrong |
@danmosemsft, wondering if the "empty" section case is also a "bigger" bug. Currently, there seems to be a lot of "weird" behavior around the section separator and it doesn't seem to be consistent. For example, from the docs for the "three section" case:
The "two section" case doesn't have any explicit wording on it, but I would imagine it would/should follow the same logic. For "-1234.0" and various format specifiers, we currently get the following:
I would expect:
|
CC. @dotnet/dnceng and @adiaaida. The various perf tests keep failing with
|
@adiaaida if you don't mind, can you please take an initial look and if it's not specific to the perf lab, open up a core-eng First-Responder issue. |
This looks like the VMs are disconnecting. We'll open a core-eng issue. |
Created https://github.com/dotnet/core-eng/issues/4228. Based off the logs, either it's very unlucky or it seems installing buildtools might actually be causing something bad to happen. |
CC. @stephentoub. It was indicated that you might have some useful input on the above (#20109 (comment)) |
@jkotas, any other feedback on this? |
This is fixing the break, but introducing a different one. I do not have a good idea about the custom formatting strings to tell whether it is the right thing to do. Would it make sense to fix the original break, without introducing a new one? |
I can do that, and then log a bug tracking the potential change required for the section modifiers. |
Logged https://github.com/dotnet/corefx/issues/32464 for the remaining questions |
@jkotas, any other feedback here? |
src/System.Private.CoreLib/shared/System/Text/ValueStringBuilder.cs
Outdated
Show resolved
Hide resolved
…igits being returned
This ensures that the sign is only printed if we are returning one or more digits.