Why does Double.ToString("F1") behave differently in .NET Core 3.1?

Viewed 203

I've updated a project from .NET Core 2.2 to .NET Core 3.1, and some of my unit tests are now failing in a surprising way.

For example, one of our tests is checking the formatting of a number (which should be rounded to 1 decimal place). The test has an input value of 1.65 and is expecting the result to be "1.7".

I've pared back the unit test to just the following, and it fails (the actual result is "1.6"):

Assert.Equal("1.7", 1.65.ToString("F1"));

I'm aware that floating-point handling was changed in .NET Core 3. However, from the description of those changes it looks like the differences should only affect the 15th decimal place.

I can only think that perhaps 1.65 is one of those numbers that's hard to represent in floating-point, and that it's actually being represented as 1.649999999999999 in the new world, which would indeed lead to it being rounded down?

Note: when I use the C# interactive console and enter 1.65.ToString("F1") it comes out as "1.7"!

1 Answers
Related