Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
math
round
communitycreator

What is Math.Round() in C#?

Arslan Bajwa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

C# has a built-in Math class which provides useful mathematical functions and operations. The class has the Round() function, which is used to round a value to the nearest integer or to the specified number of fractional digits.

Variants

Round (Decimal)

This variant rounds a decimal param to a nearest integer, and rounds the midpoint to the nearest even number. It returns a decimal type number.

Syntax
  public static decimal Round (decimal param);

where param is a decimal type input number to be rounded.

Round(Double)

It rounds a double-precision floating-point param to the nearest integer, and rounds the midpoint to the nearest even number. It returns a double type number.

Syntax

  public static double Round (double param);

where:

  • param is a double type input number to be rounded.

Round (Decimal, Int32)

This variant rounds a decimal param to a specified number of fractional digits, and rounds the midpoint to the nearest even number. It returns a decimal type number.

Syntax

  public static decimal Round (decimal param, int digits);

where:

  • param is a decimal type input number to be rounded.

  • digits is an Int32 type input number that specifies the number of decimal places in the output.

Round (Double, Int32)

This variant rounds a double-precision floating-point param to a specified number of fractional digits, and rounds the midpoint to the nearest even number. It returns a double type number.

Syntax

  public static double Round (double param, int digits);

where:

  • param is a double type input number to be rounded.

  • digits is an Int32 type input number that specifies the number of decimal places in the output.

Round (Decimal, MidpointRounding)

This variant rounds a decimal param to an integer according to the specified rounding convention. It returns a decimal type number.

Syntax

  public static decimal Round (decimal param, MidpointRounding type);

where:

  • param is a decimal type input number to be rounded.

  • type specifies the rounding strategy to be used.

Round (Double, MidpointRounding)

This variant rounds a double-precision floating-point param to an integer according to the specified rounding convention. It returns a double type number.

Syntax

  public static double Round (double param, MidpointRounding type);

where:

  • param is a double type input number to be rounded.

  • type specifies the rounding strategy to be used.

  • Round (Decimal, Int32, MidpointRounding)

    It rounds a decimal param to a specified number of fractional digits according to the specified rounding convention. It returns a decimal type number.

Syntax

  public static decimal Round (decimal param, int digits, MidpointRounding type);

where:

  • param is a decimal type input number to be rounded.

  • digits is an Int32 type input number that specifies the number of decimal places in the output.

  • type specifies the rounding strategy to be used.

Round (Double, Int32, MidpointRounding)

This rounds a double-precision floating-point param to a specified number of fractional digits according to the specified rounding convention. It returns a double type number.

Syntax

  public static double Round (double param, int digits, MidpointRounding type);

where:

  • param is a double type input number to be rounded.

  • digits is an Int32 type input number that specifies the number of decimal places in the output.

  • type specifies the rounding strategy to be used.

If param has fewer fractional digits than digits, then param is returned unchanged.

MidpointRounding methods

  • ToEven: Rounding strategy towards the nearest even number.
  • AwayFromZero: Rouding strategy towards the nearest number that’s away from zero.
  • ToZero: Rounding strategy towards zero when result is closest to and no greater in magnitude than the infinitely precise result.
  • ToNegativeInfinity: Downward-directed rounding strategy when result is closest to and no greater than the infinitely precise result.
  • ToPositiveInfinity: Upwards-directed rounding strategy when result is closest to and no less than the infinitely precise result.

Code

using System;
class Educative
{
    static void Main()
    {
        System.Console.WriteLine(Math.Round(10.2563, 2));

        System.Console.WriteLine(Math.Round(10.8, 0, MidpointRounding.ToEven));
        
        System.Console.WriteLine(Math.Round(10.8, MidpointRounding.AwayFromZero));
        
        System.Console.WriteLine(Math.Round(10.6));
    }
}

RELATED TAGS

c#
math
round
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring