Languages
[Edit]
EN

C# / .NET - Math.Round() method example

0 points
Created by:
Zeeshan-Peel
850

Math.Round is a static method that returns a number that is rounded to the closest integer number.

using System;

public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine( Math.Round( 5.0  )); //  5
                                       
        Console.WriteLine( Math.Round( 2.49 )); //  2
        Console.WriteLine( Math.Round( 2.50 )); //  3
        Console.WriteLine( Math.Round( 2.51 )); //  3
                           
        Console.WriteLine( Math.Round(-2.49 )); // -2
        Console.WriteLine( Math.Round(-2.50 )); // -2
        Console.WriteLine( Math.Round(-2.51 )); // -3
                           
        Console.WriteLine( Math.Round( 0.999 )); //  1
        Console.WriteLine( Math.Round( 1.001 )); //  1
        Console.WriteLine( Math.Round(-1.001 )); // -1
    }
}

1. Documentation

Syntax

namespace System
{
    public static class Math
    {
        // ...
        public static decimal Round(decimal d) { ... }
        public static decimal Round(decimal d, int precision) { ... }
        public static decimal Round(decimal d, int precision, MidpointRounding mode) { ... }
        public static decimal Round(decimal d, MidpointRounding mode) { ... }
        public static double Round(double value) { ... }
        public static double Round(double value, int precision) { ... }
        public static double Round(double value, int precision, MidpointRounding mode) { ... }
        public static double Round(double value, MidpointRounding mode) { ... }
        // ...
    }
}

 

SyntaxSyntax example is above.
Parameters

d - decimal value to round.

value - double value to round.

precision - number of decimal places to which we want to round.

ResultRounded number value (primitive value).
DescriptionRound is a static method that returns a number that is rounded to the closest integer number.

2. Custom round method examples

2.1. Rounding with precision to n places example

This section contains a custom function that shows how to round number with precision to n places.

using System;

public class Program
{
    static double RoundPrecised(double number, int precision)
    {
        double power = Math.Pow(10, precision);

        return Math.Round(number * power) / power;
    }

    public static void Main(string[] args)
    {
        Console.WriteLine( RoundPrecised(     5  ,  0 ) ); // 5
        Console.WriteLine( RoundPrecised(     5.0,  0 ) ); // 5
        Console.WriteLine( RoundPrecised(     0.5,  0 ) ); // 0

        Console.WriteLine( RoundPrecised(  1.2345,  0 ) ); // 1
        Console.WriteLine( RoundPrecised(  1.2345,  1 ) ); // 1.2
        Console.WriteLine( RoundPrecised(  1.2345,  2 ) ); // 1.23
        Console.WriteLine( RoundPrecised(  1.2345,  3 ) ); // 1.234

        Console.WriteLine( RoundPrecised( -1.2345,  0 ) ); // -1
        Console.WriteLine( RoundPrecised( -1.2345,  1 ) ); // -1.2
        Console.WriteLine( RoundPrecised( -1.2345,  2 ) ); // -1.23
        Console.WriteLine( RoundPrecised( -1.2345,  3 ) ); // -1.234

        Console.WriteLine( RoundPrecised(   12345, -1 ) ); // 12340
        Console.WriteLine( RoundPrecised(   12345, -2 ) ); // 12300
        Console.WriteLine( RoundPrecised(   12345, -3 ) ); // 12000
    }
}

2.2. Round implementation example

This section contains custom round function implementation.

using System;

public class Program
{
    static double RoundNumber(double value)
    {
        if (value < 0.0)
        {
            double rest = (value % 1.0);

            if (rest < -0.5)
            {
                rest += 1.0;
            }

            return value - rest;
        }
        else
        {
            value += 0.5;

            return value - (value % 1.0);
        }
    }

    public static void Main(string[] args)
    {
        // Example:

        Console.WriteLine( RoundNumber(  5     ) ); //  5

        Console.WriteLine( RoundNumber(  2.49  ) ); //  2
        Console.WriteLine( RoundNumber(  2.50  ) ); //  3
        Console.WriteLine( RoundNumber(  2.51  ) ); //  3

        Console.WriteLine( RoundNumber(  0.999 ) ); //  1
        Console.WriteLine( RoundNumber(  1.001 ) ); //  1

        Console.WriteLine( RoundNumber( -2.49  ) ); // -2
        Console.WriteLine( RoundNumber( -2.50  ) ); // -2
        Console.WriteLine( RoundNumber( -2.51  ) ); // -3

        Console.WriteLine( RoundNumber( -1.001 ) ); // -1
    }
}
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# / .NET - Math object

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join