Rounding

c# Version

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
 
            /*----------------------------------------------------------*/
            Console.WriteLine("Decimal2");
            /*----------------------------------------------------------*/
            double a = 6.283;
 
            Console.WriteLine("Before Round: " + a);
            Console.WriteLine("After Rounding: " + Math.Round(a, 2));
 
            a = 6.283;
            Console.WriteLine("Before Floor: " + a);
            Console.WriteLine("After Floor: " + Math.Floor(a));
 
            a = 6.283;
            Console.WriteLine("Before Ceiling: " + a);
            Console.WriteLine("After Ceiling: " + Math.Ceiling(a));
            /*----------------------------------------------------------*/
            Console.WriteLine("Decimal1");
            /*----------------------------------------------------------*/
            a = 6.283;
            Console.WriteLine("Before Round: " + a);
            Console.WriteLine("After Rounding: " + Math.Round(a, 1));
 
            a = 6.283;
            Console.WriteLine("Before Floor: " + a);
            Console.WriteLine("After Floor: " + Math.Floor(a));
 
            a = 6.283;
            Console.WriteLine("Before Ceiling: " + a);
            Console.WriteLine("After Ceiling: " + Math.Ceiling(a));
 
            /*----------------------------------------------------------*/
            Console.WriteLine("Decimal0");
            /*----------------------------------------------------------*/
            a = 6.283;
            Console.WriteLine("Before Round: " + a);
            Console.WriteLine("After Rounding: " + Math.Round(a));
 
            a = 6.283;
            Console.WriteLine("Before Floor: " + a);
            Console.WriteLine("After Floor: " + Math.Floor(a));
 
            a = 6.283;
            Console.WriteLine("Before Ceiling: " + a);
            Console.WriteLine("After Ceiling: " + Math.Ceiling(a));
 
 
            Console.ReadLine();
        }
    }
}

c++ Version

#include <iostream>
#include <string>
 
//using namespace std; 
 
int main (int argc, const char * argv[])
{
 
     // Normales runden
    std::cout << "Runden" << std::endl;
    std::cout << "round(+2.3) = " << std::round(2.3) // abgerundet
              << "  round(+2.5) = " << std::round(2.5) // aufgerundet
              << "  round(+2.7) = " << std::round(2.7) << '\n' // aufgerundet
 
              << "round(-2.3) = " << std::round(-2.3) // abgerundet (-2)
              << "  round(-2.5) = " << std::round(-2.5) // aufgerundet (-3)
              << "  round(-2.7) = " << std::round(-2.7) << '\n'; // aufgerundet (-3)
 
    std::cout << "round(-0.0) = " << std::round(-0.0)  << '\n' // 0
              << "round(-Inf) = " << std::round(-INFINITY) << '\n'; // -infinity
    /*------------------------------------------------------------------------------------------------------------*/
    std::cout << "#####################################################" << std::endl; 
    /*------------------------------------------------------------------------------------------------------------*/
 
    // lround
    std::cout << "lRunden" << std::endl;
    std::cout << "lround(+2.3) = " << std::lround(2.3) // abgerundet
              << "  lround(+2.5) = " << std::lround(2.5) // aufgerundet 
              << "  lround(+2.7) = " << std::lround(2.7) << '\n' // aufgerundet 
              << "lround(-2.3) = " << std::lround(-2.3) // abgerundet (-2)
              << "  lround(-2.5) = " << std::lround(-2.5) // aufgerundet (-3)
              << "  lround(-2.7) = " << std::lround(-2.7) << '\n'; // aufgerundet (-3)
 
    std::cout << "lround(-0.0) = " << std::lround(-0.0)  << '\n' // 0
              << "lround(-Inf) = " << std::lround(-INFINITY) << '\n'; // 0
 
    /*------------------------------------------------------------------------------------------------------------*/
    std::cout << "#####################################################" << std::endl; 
    /*------------------------------------------------------------------------------------------------------------*/
 
    // floor runden
    std::cout << "Floor Runden" << std::endl;
     std::cout << std::fixed
              << "floor(+2.7) = " << std::floor(+2.7) << '\n' // abgerundet (2)
              << "floor(-2.7) = " << std::floor(-2.7) << '\n' // aufgerundet ( also abgerundet) (-3)
              << "floor(-0.0) = " << std::floor(-0.0) << '\n' // bleibt 0
              << "floor(-Inf) = " << std::floor(-INFINITY) << '\n'; // -inf
 
    /*------------------------------------------------------------------------------------------------------------*/
    std::cout << "#####################################################" << std::endl; 
    /*------------------------------------------------------------------------------------------------------------*/
    
    // ceiling runden
    std::cout << "Ceiling Runden" << std::endl; 
    std::cout << std::fixed
              << "ceil(+2.4) = " << std::ceil(+2.4) << '\n' // aufgerundet (3)
              << "ceil(-2.4) = " << std::ceil(-2.4) << '\n' // abgerundet ( also aufgerundet) (-2)
              << "ceil(-0.0) = " << std::ceil(-0.0) << '\n' // bleibt 0 
              << "ceil(-Inf) = " << std::ceil(-INFINITY) << '\n'; // -inf    
 
    /*------------------------------------------------------------------------------------------------------------*/
 
    double Zahl = 5.4562;
    double Stellen = 2;
 
    Zahl *= pow( 10, Stellen);
    if (Zahl >= 0)
        Zahl = floor(Zahl + 0.5);
    else
        Zahl = ceil(Zahl - 0.5);
    Zahl *= pow(10, -Stellen);
    std::cout << Zahl << std::endl;
    return Zahl;
 
}
2 Likes

Awesome job implementing math into C# and C++

What I expected:

Math.Round(6.283,1) -> 6.3
Math.Round(6.283,2) -> 6.30
Math.Floor (6.283) -> 6.0
Math.Ciel(6.283) -> 7.0

What I got:

Math.Round(6.283,1) -> 6.3
Math.Round(6.283,2) -> 6.28
Math.Floor (6.283) -> 6
Math.Ciel(6.283) -> 7

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MathForGamesOne : MonoBehaviour
{ // Update is called once per frame
    void Update()
    {
        float a = 6.283f;
        Debug.Log("Our Number is "+ a);
        Debug.Log("ROUND 1...FIGHT! " + Math.Round(a,1));
        Debug.Log("ROUND 2...FIGHT! " + Math.Round(a,2));
        
        Debug.Log("Let the bodies hit the.... " + Math.Floor(a));
        Debug.Log("I before E except after... " + Math.Ceiling(a));
    }
}

2 Likes

Privacy & Terms