0
using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Rounding: {0:f2}", 34346.335);
    }
}

I am aware of .NET's default rounding behavior (to even / banker's rule), so I expected to see "Rounding: 34346.34".

  • On .NET 4.7.2, I get what I expected.
  • On .NET 5.0, "Rounding: 34346.33" is printed instead.

Did I miss something here? Or is it a known bug?

JayTee
  • 163
  • 1
  • 6
  • 2
    Does this answer your question? [Rounding differences between decimal and double when formatting with ToString()](https://stackoverflow.com/questions/63300591/rounding-differences-between-decimal-and-double-when-formatting-with-tostring) – Lukasz Szczygielek May 04 '21 at 14:31
  • 1
    It is a known change since .net core 3, it has been discussed in detail here: https://stackoverflow.com/questions/60147860/rounding-issues-net-core-3-1-vs-net-core-2-0-net-framework – Thomas Schmidt May 04 '21 at 14:33
  • @Hostel Rounding 34346.335 to even resp. away from zero should yield the same result (34346.34), so I don't see why it would give me 34346.33 at all. – JayTee May 04 '21 at 15:17
  • Okay, I think I've got a clue now; It is the old FP problem, my number (34346.335) is not exactly representable and becomes something like 34346.33499999999912688509. And that one goes down with AwayFromZero and up with ToEven. Thx for your answers! – JayTee May 04 '21 at 15:54

0 Answers0