1

Consider the following program

    public static void Main()
    {
        double result = 0;
        if (Double.TryParse("29.97999999999999", NumberStyles.Any, CultureInfo.InvariantCulture, out result)) {         
            Console.WriteLine(result);
        }
    }

When run in .net framework (x64) it prints 29.98 whereas in .net core it prints 29.97999999999999.

Any idea why that is? And more important, how can I force one of the frameworks to round like the other and vice-versa?

Dan M
  • 770
  • 1
  • 9
  • 18
  • 5
    i would guess it's because they [changed floating point handling in .net core 3.0](https://devblogs.microsoft.com/dotnet/floating-point-parsing-and-formatting-improvements-in-net-core-3-0/) - and i don't think there's a switch to force one behaviour. but then again: why would you need one? floats should always be treated as inherently imprecise, because that's what they are; if you need arbitrary precision, better use `Decimal` – Franz Gleichmann Dec 12 '20 at 15:11
  • 2
    This problem is discussed here: https://stackoverflow.com/questions/60147860/rounding-issues-net-core-3-1-vs-net-core-2-0-net-framework. It does not have an accepted answer though. – Roar S. Dec 12 '20 at 15:12

0 Answers0