Skip to content

Floating-point Formatting and Parsing behavior changed for .NET Core 3.0 #14350

@tannergooding

Description

@tannergooding

Floating-point Formatting and Parsing behavior changed for .NET Core 3.0

See Floating-point formatting and parsing behavior changed for updated documentation for this change.

Floating-point (System.Double and System.Single) formatting and parsing were changed to be IEEE compliant for .NET Core 3.0

Version introduced

.NET Core 3.0

Old behavior

Formatting (ToString) and Parsing (Parse and TryParse) were previously not IEEE compliant and it was impossible to guarantee that a value would roundtrip under any supported standard or custom numeric format string.

New behavior

Formatting and Parsing were updated to be IEEE compliant. This ensures the framework is compliant with the Runtime/Framework specs that dictate we are IEEE 754 compliant and also ensures we match the behavior performed by IEEE compliant languages (such as C#) when they deal with the same string as a "literal" (e.g. float f = 1.0f where 1.0f is a literal).

Reason for change

Previously users would not get a string that roundtrips by default. For example, take the following code:

float f = value;
string s = f.ToString();
float r = float.Parse(s);
bool roundtrips = (f == r);

For some inputs, float.Parse(s) could fail. For other inputs, roundtrips would be false.

Recommended action

Users should read https://devblogs.microsoft.com/dotnet/floating-point-parsing-and-formatting-improvements-in-net-core-3-0/ and review it for the suggested changes. Generally this involves using a different format string (either standard or custom) to enforce getting the desired behavior. Some of the results may not have a workaround as they were previously just incorrect.

Category

This impacts multiple categories from the below as it impacts core methods on a primitive type.

[Choose a category from one of the following:

  • ASP.NET Core
  • C#
  • Core
  • CoreFx
  • Data
  • Debugger
  • Deployment for .NET Core
  • Globalization
  • interop
  • JIT
  • LINQ
  • Managed Extensibility Framework (MEF)
  • MSBuild
  • Networking
  • Printing
  • Security
  • Serialization
  • Visual Basic
  • Windows Forms
  • Windows Presentation Foundation (WPF)
  • XML, XSLT
    ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking-changeIndicates a .NET Core breaking change

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions