Updating NumberToStringFormat to not print the sign if there are no digits being...
authorTanner Gooding <tagoo@outlook.com>
Fri, 28 Sep 2018 18:02:55 +0000 (11:02 -0700)
committerGitHub <noreply@github.com>
Fri, 28 Sep 2018 18:02:55 +0000 (11:02 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/98aff4a23a2ecf8d823ece9b201045f52aa0624a

src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs
src/libraries/System.Private.CoreLib/src/System/Text/ValueStringBuilder.cs

index e9c21a7..ed0fdb6 100644 (file)
@@ -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)
index 74b5dac..30c94cb 100644 (file)
@@ -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)
         {