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.
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.
public static decimal Round (decimal param);
where param
is a decimal type input number to be rounded.
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.
public static double Round (double param);
where:
param
is a double type input number to be rounded.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.
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.
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.
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.
This variant rounds a decimal param
to an integer according to the specified rounding convention. It returns a decimal
type number.
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.
This variant rounds a double-precision floating-point param
to an integer according to the specified rounding convention. It returns a double type number.
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.
param
to a specified number of fractional digits according to the specified rounding convention. It returns a decimal type number. 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.
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.
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 thandigits
, thenparam
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.
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));}}