From 92ccfe931f12957460c8355d8a90a89df1bc2f65 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Fri, 28 Sep 2018 11:02:55 -0700 Subject: [PATCH] Updating NumberToStringFormat to not print the sign if there are no digits being returned (dotnet/coreclr#20109) Commit migrated from https://github.com/dotnet/coreclr/commit/98aff4a23a2ecf8d823ece9b201045f52aa0624a --- .../src/System/Number.Formatting.cs | 7 +++++-- .../src/System/Text/ValueStringBuilder.cs | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs b/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs index e9c21a7..ed0fdb6 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs @@ -1770,8 +1770,8 @@ SkipRounding: } } } - - if (number.sign && section == 0) + + if (number.sign && (section == 0) && (number.scale != 0)) sb.Append(info.NegativeSign); bool decimalWritten = false; @@ -1929,6 +1929,9 @@ SkipRounding: } } } + + if (number.sign && (section == 0) && (number.scale == 0) && (sb.Length > 0)) + sb.Insert(0, info.NegativeSign); } private static void FormatCurrency(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info) diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/ValueStringBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Text/ValueStringBuilder.cs index 74b5dac..30c94cb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/ValueStringBuilder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/ValueStringBuilder.cs @@ -139,6 +139,21 @@ namespace System.Text _pos += count; } + public void Insert(int index, string s) + { + int count = s.Length; + + if (_pos > (_chars.Length - count)) + { + Grow(count); + } + + int remaining = _pos - index; + _chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count)); + s.AsSpan().CopyTo(_chars.Slice(index)); + _pos += count; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Append(char c) { -- 2.7.4