Skip to content
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

Update standard numeric format parsing to handle higher precision #22458

Closed
pgovind opened this issue Jan 22, 2021 · 0 comments · Fixed by #23046
Closed

Update standard numeric format parsing to handle higher precision #22458

pgovind opened this issue Jan 22, 2021 · 0 comments · Fixed by #23046
Assignees
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 6 Issues and PRs for the .NET 6 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3

Comments

@pgovind
Copy link
Contributor

pgovind commented Jan 22, 2021

Update the parsing/formatting logic to allow for precision more than 99 digits

Currently, the standard numeric format parsing logic is limited to precision <= 99. Some numeric types have more precision, but ToString(string format) does not expose it correctly. With dotnet/runtime#46874, we now support precision up to int.MaxValue. Precision > int.MaxValue will throw a FormatException. Since the change occurs in the parsing logic, this will affect all numeric types.

Version introduced

.NET 6 Preview 2

Old behavior

Did not support precision > 99. For ex: 32.ToString("C100") will print C132. This occurred because the parse routine interpreted precision more than 99 as a custom numeric format string. Consequently formats such as H100 would be interpreted as a custom format and print wrong values while H99 would throw because it was (correctly) interpreted as an unsupported standard format

New behavior

The new rule is: A format modifier with any number of digits is interpreted as a standard format + precision. If the value doesn't fit into an int, we throw.
For ex:
32.ToString("C100") will print $32.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Unsupported formats such as 32.ToString("H100") will throw a FormatException.

Reason for change

Fix unexpected behavior when using higher precision.

Recommended action

If you are using formats such as G142 today, you are technically encountering a bug. If you want to maintain the previous behavior (i.e. where 42.ToString("G999") was intentionally meant to return G999), you can explicitly single quote the first character (i.e. 42.ToString("'G'999")). This will work on .NET Framework, .NET Core, and .NET 5+

Format strings, like the following, would continue to be interpreted as "custom numeric format strings":

  • start with any character that is not an ASCII alphabetical character (such as $ or è)
  • start with an ASCII alphabetical character but which are not followed by an ASCII digit (such as A$)
  • start with an ASCII alphabetical character, followed by an ASCII digit sequence, and then any character that is not an ASCII digit character (such as A55A).

Category

  • [ * ] Core .NET libraries

Affected APIs

BigInteger.ToString(string format)
BigInteger.ToString(string format, IFormatProvider provider)
BigInteger.TryFormat()
// Integral Types
int/uint/byte/sbyte/short/ushort/long/ulong.ToString(string format)
int/uint/byte/sbyte/short/ushort/long/ulong.ToString(string format, IFormatProvider provider)
int/uint/byte/sbyte/short/ushort/long/ulong.TryFormat()
// Floating Types
half/single/double/decimal.ToString(string format)
half/single/double/decimal.ToString(string format, IFormatProvider provider)
half/single/double/decimal.TryFormat()

Issue metadata

  • Issue type: breaking-change
@PRMerger6 PRMerger6 added the Pri3 label Jan 22, 2021
@dotnet-bot dotnet-bot added ⌚ Not Triaged Not triaged breaking-change Indicates a .NET Core breaking change doc-idea Indicates issues that are suggestions for new topics [org][type][category] labels Jan 22, 2021
@gewarren gewarren added 🏁 Release: .NET 6 Issues and PRs for the .NET 6 release Pri1 High priority, do before Pri2 and Pri3 and removed ⌚ Not Triaged Not triaged Pri3 labels Feb 11, 2021
@gewarren gewarren self-assigned this Feb 11, 2021
@gewarren gewarren added this to To do in February 2021 via automation Feb 23, 2021
February 2021 automation moved this from To do to Done Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 6 Issues and PRs for the .NET 6 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3
Projects
No open projects
February 2021
  
Done
Development

Successfully merging a pull request may close this issue.

4 participants