-
Notifications
You must be signed in to change notification settings - Fork 6k
Limit precision to 999,999,999 #31002
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
docs/core/compatibility/core-libraries/7.0/max-precision-numeric-format-strings.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
--- | ||
title: "Breaking change: Maximum precision for numeric format strings" | ||
description: Learn about the .NET 7 breaking change in core .NET libraries where the maximum precision for numeric format strings was changed to 999,999,999. | ||
ms.date: 09/02/2022 | ||
--- | ||
# Maximum precision for numeric format strings | ||
|
||
The maximum precision when formatting numbers as strings using `ToString` and `TryFormat` has been changed from <xref:System.Int32.MaxValue?displayProperty=nameWithType> to 999,999,999. (The maximum precision was [previously changed](../6.0/numeric-format-parsing-handles-higher-precision.md) to <xref:System.Int32.MaxValue?displayProperty=nameWithType> in .NET 6.) | ||
|
||
In addition, the maximum exponent allowed when parsing a <xref:System.Numerics.BigInteger> from a string has been limited to 999,999,999. | ||
|
||
## Previous behavior | ||
|
||
In .NET 6, the standard numeric format parsing logic was limited to a precision of <xref:System.Int32.MaxValue?displayProperty=nameWithType> or less. The intended behavior was to throw a <xref:System.FormatException> for any precision larger than <xref:System.Int32.MaxValue?displayProperty=nameWithType>. However, due to a bug, .NET 6 didn't throw that exception for some such inputs. The intended behavior was: | ||
|
||
```csharp | ||
double d = 123.0; | ||
|
||
d.ToString("E" + int.MaxValue.ToString()); // Doesn't throw. | ||
|
||
long intMaxPlus1 = (long)int.MaxValue + 1; | ||
string intMaxPlus1String = intMaxPlus1.ToString(); | ||
Assert.Throws<FormatException>(() => d.ToString("E" + intMaxPlus1String)); // Throws. | ||
``` | ||
|
||
In addition, there was no limit on the exponent size when parsing a <xref:System.Numerics.BigInteger> from a string. | ||
|
||
## New behavior | ||
|
||
Starting in .NET 7, .NET supports precision up to 999,999,999. A <xref:System.FormatException> is thrown if the precision is greater than 999,999,999. This change was implemented in the parsing logic that affects all numeric types. | ||
|
||
```csharp | ||
double d = 123.0; | ||
Assert.Throws<FormatException>(() => d.ToString("E" + int.MaxValue.ToString())); // Throws. | ||
|
||
long intMaxPlus1 = (long)int.MaxValue + 1; | ||
string intMaxPlus1String = intMaxPlus1.ToString(); | ||
Assert.Throws<FormatException>(() => d.ToString("E" + intMaxPlus1String)); // Throws. | ||
|
||
d.ToString("E999999999"); // Doesn't throw. | ||
|
||
d.ToString("E00000999999999"); // Doesn't throw. | ||
``` | ||
|
||
In addition, if you attempt to parse a <xref:System.Numerics.BigInteger> with an exponent greater than 999,999,999 from a string, a <xref:System.FormatException> is thrown. | ||
|
||
## Version introduced | ||
|
||
.NET 7 RC 1 | ||
|
||
## Type of breaking change | ||
|
||
This change can affect [binary compatibility](../../categories.md#binary-compatibility). | ||
|
||
## Reason for change | ||
|
||
The behavior that was introduced in .NET 6 was intended to throw a <xref:System.FormatException> for any precision larger than <xref:System.Int32.MaxValue?displayProperty=nameWithType>. However, due to a bug, it did not throw that exception for some inputs larger than the maximum. This change fixes the bug by limiting the precision to 999,999,999. | ||
|
||
## Recommended action | ||
|
||
In most cases, no action is necessary, because it's unlikely that you're already using a precision higher than 999,999,999 in your format strings. | ||
|
||
## Affected APIs | ||
|
||
This change was implemented in the parsing logic that affects all numeric types. | ||
|
||
- <xref:System.Numerics.BigInteger.ToString(System.String)?displayProperty=fullName> | ||
- <xref:System.Numerics.BigInteger.ToString(System.String,System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Numerics.BigInteger.TryFormat(System.Span{System.Char},System.Int32@,System.ReadOnlySpan{System.Char},System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Int32.ToString(System.String)?displayProperty=fullName> | ||
- <xref:System.Int32.ToString(System.String,System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Int32.TryFormat(System.Span{System.Char},System.Int32@,System.ReadOnlySpan{System.Char},System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.UInt32.ToString(System.String)?displayProperty=fullName> | ||
- <xref:System.UInt32.ToString(System.String,System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.UInt32.TryFormat(System.Span{System.Char},System.Int32@,System.ReadOnlySpan{System.Char},System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Byte.ToString(System.String)?displayProperty=fullName> | ||
- <xref:System.Byte.ToString(System.String,System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Byte.TryFormat(System.Span{System.Char},System.Int32@,System.ReadOnlySpan{System.Char},System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.SByte.ToString(System.String)?displayProperty=fullName> | ||
- <xref:System.SByte.ToString(System.String,System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.SByte.TryFormat(System.Span{System.Char},System.Int32@,System.ReadOnlySpan{System.Char},System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Int16.ToString(System.String)?displayProperty=fullName> | ||
- <xref:System.Int16.ToString(System.String,System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Int16.TryFormat(System.Span{System.Char},System.Int32@,System.ReadOnlySpan{System.Char},System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.UInt16.ToString(System.String)?displayProperty=fullName> | ||
- <xref:System.UInt16.ToString(System.String,System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.UInt16.TryFormat(System.Span{System.Char},System.Int32@,System.ReadOnlySpan{System.Char},System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Numerics.BigInteger.Parse%2A?displayProperty=fullName> | ||
- <xref:System.Numerics.BigInteger.TryParse%2A?displayProperty=fullName> | ||
- <xref:System.Int64.ToString(System.String)?displayProperty=fullName> | ||
- <xref:System.Int64.ToString(System.String,System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Int64.TryFormat(System.Span{System.Char},System.Int32@,System.ReadOnlySpan{System.Char},System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.UInt64.ToString(System.String)?displayProperty=fullName> | ||
- <xref:System.UInt64.ToString(System.String,System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.UInt64.TryFormat(System.Span{System.Char},System.Int32@,System.ReadOnlySpan{System.Char},System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Half.ToString(System.String)?displayProperty=fullName> | ||
- <xref:System.Half.ToString(System.String,System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Half.TryFormat(System.Span{System.Char},System.Int32@,System.ReadOnlySpan{System.Char},System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Single.ToString(System.String)?displayProperty=fullName> | ||
- <xref:System.Single.ToString(System.String,System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Single.TryFormat(System.Span{System.Char},System.Int32@,System.ReadOnlySpan{System.Char},System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Double.ToString(System.String)?displayProperty=fullName> | ||
- <xref:System.Double.ToString(System.String,System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Double.TryFormat(System.Span{System.Char},System.Int32@,System.ReadOnlySpan{System.Char},System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Decimal.ToString(System.String)?displayProperty=fullName> | ||
- <xref:System.Decimal.ToString(System.String,System.IFormatProvider)?displayProperty=fullName> | ||
- <xref:System.Decimal.TryFormat(System.Span{System.Char},System.Int32@,System.ReadOnlySpan{System.Char},System.IFormatProvider)?displayProperty=fullName> | ||
|
||
## See also | ||
|
||
- [Standard numeric format parsing precision breaking change (.NET 6)](../6.0/numeric-format-parsing-handles-higher-precision.md) | ||
- [Standard numeric format strings](../../../../standard/base-types/standard-numeric-format-strings.md) | ||
- [Character literals in custom format strings](../../../../standard/base-types/custom-numeric-format-strings.md#character-literals) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.