'Need a .NET Framework solution for IEEE-754 float/double parse and ToString

.NET framework does not give me expected results for float/double parsing and ToString. It is fixed in .NET 6 (even in .NET Core 3.0). However, I cannot migrate my whole project from .NET framework 4.X to 6.

Here an example. 0x5a0e1bca is 10000000272564224 in float representation. I executed following code using both .NET Framework 4.7.2 and .NET 6.

var fl = BitConverter.ToSingle(new byte[] { 0xCA, 0x1B, 0x0E, 0x5A }, 0);
Console.WriteLine(fl.ToString("g20"));

.NET Framework 4.7.2 output:

10000000300000000

.NET 6 output:

10000000272564224

10000000300000000 is not even represented in float.

Does anybody know why this happens and how I can overcome it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source