The System.Math struct provides static methods for trigonometric, logarithmic, and other common mathematical functions. The methods discussed here specifically handle rounding of numeric values to the nearest integer or to a specified number of decimal places.
These methods are crucial for controlling precision and presenting numerical data in a human-readable format. Understanding the different rounding behaviors (e.g., round to nearest, round up, round down, round to even) is essential for accurate calculations and data representation.
Math.Round(decimal)public static decimal Round(decimal d)
Rounds a decimal number to the nearest whole number. If the fractional part is exactly 0.5, it rounds to the nearest even number.
d: A decimal number to round.The nearest whole number to the value of d. If the fractional part is exactly 0.5, it rounds to the nearest even number.
decimal value1 = 2.3m;
decimal roundedValue1 = Math.Round(value1); // Result: 2
decimal value2 = 2.7m;
decimal roundedValue2 = Math.Round(value2); // Result: 3
decimal value3 = 2.5m;
decimal roundedValue3 = Math.Round(value3); // Result: 2 (rounds to nearest even)
decimal value4 = 3.5m;
decimal roundedValue4 = Math.Round(value4); // Result: 4 (rounds to nearest even)
Math.Round(double)public static double Round(double d)
Rounds a double-precision floating-point number to the nearest whole number. If the fractional part is exactly 0.5, it rounds to the nearest even number.
d: A double number to round.The nearest whole number to the value of d. If the fractional part is exactly 0.5, it rounds to the nearest even number.
double value1 = 2.3;
double roundedValue1 = Math.Round(value1); // Result: 2.0
double value2 = 2.7;
double roundedValue2 = Math.Round(value2); // Result: 3.0
double value3 = 2.5;
double roundedValue3 = Math.Round(value3); // Result: 2.0 (rounds to nearest even)
double value4 = 3.5;
double roundedValue4 = Math.Round(value4); // Result: 4.0 (rounds to nearest even)
Math.Round(decimal, int)public static decimal Round(decimal d, int decimals)
Rounds a decimal number to a specified number of fractional digits. If the fractional part is exactly halfway between two numbers, it rounds to the nearest even number.
d: A decimal number to round.decimals: The number of decimal places to which the value is rounded.The decimal number nearest to d that has a fractional part of the length specified by decimals. If the fractional part is exactly halfway between two numbers, it rounds to the nearest even number.
decimal value1 = 123.4567m;
decimal roundedValue1 = Math.Round(value1, 2); // Result: 123.46
decimal value2 = 123.455m;
decimal roundedValue2 = Math.Round(value2, 2); // Result: 123.46 (rounds to nearest even)
decimal value3 = 123.445m;
decimal roundedValue3 = Math.Round(value3, 2); // Result: 123.44 (rounds to nearest even)
Math.Round(double, int)public static double Round(double d, int decimals)
Rounds a double-precision floating-point number to a specified number of fractional digits. If the fractional part is exactly halfway between two numbers, it rounds to the nearest even number.
d: A double number to round.decimals: The number of decimal places to which the value is rounded.The double number nearest to d that has a fractional part of the length specified by decimals. If the fractional part is exactly halfway between two numbers, it rounds to the nearest even number.
double value1 = 123.4567;
double roundedValue1 = Math.Round(value1, 2); // Result: 123.46
double value2 = 123.455;
double roundedValue2 = Math.Round(value2, 2); // Result: 123.46 (rounds to nearest even)
double value3 = 123.445;
double roundedValue3 = Math.Round(value3, 2); // Result: 123.44 (rounds to nearest even)
Math.Round(decimal, MidpointRounding)public static decimal Round(decimal d, MidpointRounding mode)
Rounds a decimal number to the nearest whole number, using the specified rounding convention for the midpoint.
d: A decimal number to round.mode: An enumeration value that specifies the rounding convention to use when the fractional part of the number is exactly 0.5.The nearest whole number to the value of d. The rounding behavior at the midpoint is determined by the mode parameter.
MidpointRounding Enum Values:ToEven: If the fractional part is exactly 0.5, the number is rounded to the nearest even integer. (Default behavior)AwayFromZero: If the fractional part is exactly 0.5, the number is rounded away from zero.
decimal value = 2.5m;
decimal roundedToEven = Math.Round(value, MidpointRounding.ToEven); // Result: 2
decimal roundedAwayFromZero = Math.Round(value, MidpointRounding.AwayFromZero); // Result: 3
Math.Round(double, MidpointRounding)public static double Round(double d, MidpointRounding mode)
Rounds a double-precision floating-point number to the nearest whole number, using the specified rounding convention for the midpoint.
d: A double number to round.mode: An enumeration value that specifies the rounding convention to use when the fractional part of the number is exactly 0.5.The nearest whole number to the value of d. The rounding behavior at the midpoint is determined by the mode parameter.
double value = 2.5;
double roundedToEven = Math.Round(value, MidpointRounding.ToEven); // Result: 2.0
double roundedAwayFromZero = Math.Round(value, MidpointRounding.AwayFromZero); // Result: 3.0
The Math.Round method has several other overloads that combine the functionality of rounding to a specific number of decimal places with a chosen MidpointRounding mode:
Math.Round(decimal, int, MidpointRounding)Math.Round(double, int, MidpointRounding)These overloads provide granular control over how numbers are rounded in complex scenarios.