Here is my code:
using static System.Console;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
double[] doubles = new[] { 9.05, 9.15, 9.25, 9.35, 9.45, 9.55, 9.65, 9.75, 9.85, 9.95 };
foreach (double n in doubles)
{
WriteLine("{0} ===> {1:F1}", n, n);
}
}
}
}
Output in .NET Framework 4.7.2
:
9.05 ===> 9.1
9.15 ===> 9.2
9.25 ===> 9.3
9.35 ===> 9.4
9.45 ===> 9.5
9.55 ===> 9.6
9.65 ===> 9.7
9.75 ===> 9.8
9.85 ===> 9.9
9.95 ===> 10.0
Output in .NET 6
(with same code):
9.05 ===> 9.1
9.15 ===> 9.2
9.25 ===> 9.2
9.35 ===> 9.3
9.45 ===> 9.4
9.55 ===> 9.6
9.65 ===> 9.7
9.75 ===> 9.8
9.85 ===> 9.8
9.95 ===> 9.9
So, in .NET Framework, the numbers are rounded just like we were taught in school. Which is called round half up
in Wikipedia.
But in .NET 6, 9.05, 9.15, 9.55, 9.65, 9.75
are rounded up, while 9.25, 9.35, 9.45, 9.85, 9.95
are rounded down.
I know there is a rule called round half to even
– rounds to the nearest value; if the number falls midway, it is rounded to the nearest value with an even least significant digit.
But this is obviously not round half to even
, some numbers are rounded to odd.
How can we explain the difference in .NET Framework 4.7.2 with .NET 6 and how can I just round the numbers in the same way as .NET Framework in .NET 6?
{1:F1}
part, but it is too late (as in bed time) for me to investigate for you. – Bushwhackround half up
rule when I change{1:F1}
to{1:.#}
. – Playroom{1:F20}
and see what happens. Skimming through @MichaelLiu's link makes me believe that those changes are the rationale for what you are seeing. Remember:float
anddouble
are inexact representations – PrecedencyBanker's Rounding
, I think .NET also use this rule if you see the answer from David Browne. – Playroom