Home > Microsoft .NET > Runden in Microsoft .NET

Runden in Microsoft .NET

Es gibt in Microsoft .NET alle gebräuchlichen Möglichkeiten, Zahlen zu runden:

using System;
 
namespace RoundTest
{
    class Program
    {
        static void TestOutput(double value)
        {
            Console.WriteLine();
            Console.Write(value);
            Console.Write("\t Truncate: " + Convert.ToInt32(Math.Truncate(value)));
            Console.Write("\t Ceiling : " + Convert.ToInt32(Math.Ceiling(value)));
            Console.Write("\t RoundAFZ: " + Convert.ToInt32(Math.Round(value, MidpointRounding.AwayFromZero)));
            Console.Write("\t RoundToE: " + Convert.ToInt32(Math.Round(value, MidpointRounding.ToEven)));
        }
 
        static void Main(string[] args)
        {
            TestOutput(10.0);
            // ...
            TestOutput(-10.51);
            Console.ReadLine();
        }
    }
}

Hier ist die Ergebnisausgabe davon zu finden:

10       Truncate: 10    Ceiling : 10    RoundAFZ: 10    RoundToE: 10
10,49    Truncate: 10    Ceiling : 11    RoundAFZ: 10    RoundToE: 10
10,5     Truncate: 10    Ceiling : 11    RoundAFZ: 11    RoundToE: 10
10,51    Truncate: 10    Ceiling : 11    RoundAFZ: 11    RoundToE: 11
 
-10      Truncate: -10   Ceiling : -10   RoundAFZ: -10   RoundToE: -10
-10,49   Truncate: -10   Ceiling : -10   RoundAFZ: -10   RoundToE: -10
-10,5    Truncate: -10   Ceiling : -10   RoundAFZ: -11   RoundToE: -10
-10,51   Truncate: -10   Ceiling : -10   RoundAFZ: -11   RoundToE: -11

kick it on dotnet-kicks.de

KategorienMicrosoft .NET Tags: ,
  1. Bisher keine Kommentare
  1. Bisher keine Trackbacks