Should 0.5 round up or down?
C#
21
Posts
7
Posters
396
Views
1
Watching
-
float n = 0.2f; Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" ); n = 0.5f; Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" ); n = 0.6f; Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" ); n = 1.5f; Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" ); n = 1.2f; Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" ); n = 1.7f; Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
Answer: 0.20: 0.00 0.50: 0.00 0.60: 1.00 1.50: 2.00 1.20: 1.00 1.70: 2.00
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
I learned in school: "When you have a 5 to round from; the number to round to shall be even." Let me give you some examples: Rounding to no decimals: 0.5 --> 0 1.5 --> 2 2.5 --> 2 3.5 --> 4 Rounding to one decimal: 0.35 --> 0.4 0.45 --> 0.4 0.55 --> 0.6 0.65 --> 0.6