diff --git a/src/libraries/Common/src/System/Globalization/FormatProvider.Number.cs b/src/libraries/Common/src/System/Globalization/FormatProvider.Number.cs index b097ff36758fd..96e177ffe4e54 100644 --- a/src/libraries/Common/src/System/Globalization/FormatProvider.Number.cs +++ b/src/libraries/Common/src/System/Globalization/FormatProvider.Number.cs @@ -517,15 +517,19 @@ private static unsafe bool ParseNumber(ref char* str, char* strEnd, NumberStyles { exp = exp * 10 + (ch - '0'); ch = ++p < strEnd ? *p : '\0'; - if (exp > 1000) - { - exp = 9999; - while (ch >= '0' && ch <= '9') - { - ch = ++p < strEnd ? *p : '\0'; - } - } - } while (ch >= '0' && ch <= '9'); + } while ((ch >= '0') && (ch <= '9') && (exp < int.MaxValue / 10)); + + if ((ch >= '0') && (ch <= '9')) + { + // We still had remaining characters but bailed early because + // the exponent was going to overflow. If exp is exactly 214748364 + // then we can technically handle one more character being 0-9 + // but the additional complexity might not be worthwhile. + + Debug.Assert(exp >= 214748364); + return false; + } + if (negExp) { exp = -exp; diff --git a/src/libraries/System.Runtime.Numerics/tests/BigInteger/BigIntegerToStringTests.cs b/src/libraries/System.Runtime.Numerics/tests/BigInteger/BigIntegerToStringTests.cs index 2bbc23476c509..acaee5e316845 100644 --- a/src/libraries/System.Runtime.Numerics/tests/BigInteger/BigIntegerToStringTests.cs +++ b/src/libraries/System.Runtime.Numerics/tests/BigInteger/BigIntegerToStringTests.cs @@ -439,6 +439,25 @@ public static void CustomFormatPerMille() RunCustomFormatToStringTests(s_random, "#\u2030000000", CultureInfo.CurrentCulture.NumberFormat.NegativeSign, 6, PerMilleSymbolFormatter); } + public static IEnumerable RunFormatScientificNotationToBigIntegerAndViceVersaData() + { + yield return new object[] { "1E+1000", "1E+1000" }; + yield return new object[] { "1E+1001", "1E+1001" }; + } + + [Theory] + [MemberData(nameof(RunFormatScientificNotationToBigIntegerAndViceVersaData))] + public static void RunFormatScientificNotationToBigIntegerAndViceVersa(string testingValue, string expectedResult) + { + BigInteger parsedValue; + string actualResult; + + parsedValue = BigInteger.Parse(testingValue, NumberStyles.AllowExponent); + actualResult = parsedValue.ToString("E0"); + + Assert.Equal(expectedResult, actualResult); + } + private static void RunSimpleProviderToStringTests(Random random, string format, NumberFormatInfo provider, int precision, StringFormatter formatter) { string test;