To rount 0.045 to 0.05, you have to use the the following Round method:
decimal.Round(0.045 , 2, MidpointRounding.AwayFromZero);
|
Midpoint values and rounding conventions
Rounding involves converting a numeric value with a specified precision to the nearest value with less precision. For example, you can use the Round(Double) method to round a value of 3.4 to 3.0, and the Round(Double, Int32) method to round a value of 3.579 to 3.58.
In a midpoint value, the value after the least significant digit in the result is precisely half way between two numbers. For example, 3.47500 is a midpoint value if it is to be rounded two decimal places, and 7.500 is a midpoint value if it is to be rounded to an integer. In these cases, the nearest value can't be easily identified without a rounding convention.
The Round method supports two rounding conventions for handling midpoint values:
Rounding away from zero
Midpoint values are rounded to the next number away from zero. For example, 3.75 rounds to 3.8, 3.85 rounds to 3.9, -3.75 rounds to -3.8, and -3.85 rounds to -3.9. This form of rounding is represented by the MidpointRounding.AwayFromZero enumeration member.
Rounding away from zero is the most widely known form of rounding.
Rounding to nearest, or banker's rounding
Midpoint values are rounded to the nearest even number. For example, both 3.75 and 3.85 round to 3.8, and both -3.75 and -3.85 round to -3.8. This form of rounding is represented by the MidpointRounding.ToEven enumeration member.
Rounding to nearest is the standard form of rounding used in financial and statistical operations. It conforms to IEEE Standard 754, section 4. When used in multiple rounding operations, it reduces the rounding error that is caused by consistently rounding midpoint values in a single direction. In some cases, this rounding error can be significant.
The following example illustrates the bias that can result from consistently rounding midpoint values in a single direction. The example computes the true mean of an array of Decimal values, and then computes the mean when the values in the array are rounded by using the two conventions. In this example, the true mean and the mean that results when rounding to nearest are the same. However, the mean that results when rounding away from zero differs by .05 (or by 3.6%) from the true mean.
using System;
public class Example
{
public static void Main()
{
decimal[] values = { 1.15m, 1.25m, 1.35m, 1.45m, 1.55m, 1.65m };
decimal sum = 0;
// Calculate true mean.
foreach (var value in values)
sum += value;
Console.WriteLine("True mean: {0:N2}", sum/values.Length);
// Calculate mean with rounding away from zero.
sum = 0;
foreach (var value in values)
sum += Math.Round(value, 1, MidpointRounding.AwayFromZero);
Console.WriteLine("AwayFromZero: {0:N2}", sum/values.Length);
// Calculate mean with rounding to nearest.
sum = 0;
foreach (var value in values)
sum += Math.Round(value, 1, MidpointRounding.ToEven);
Console.WriteLine("ToEven: {0:N2}", sum/values.Length);
}
}
// The example displays the following output:
// True mean: 1.40
// AwayFromZero: 1.45
// ToEven: 1.40
By default, the Round method uses the rounding to nearest convention. The following table lists the overloads of the Round method and the rounding convention that each uses.
|
Rounding and precision
In order to determine whether a rounding operation involves a midpoint value, the Round method multiplies the original value to be rounded by 10n, where n is the desired number of fractional digits in the return value, and then determines whether the remaining fractional portion of the value is greater than or equal to .5. This is a slight variation on a test for equality, and as discussed in the "Testing for Equality" section of the Double reference topic, tests for equality with floating-point values are problematic because of the floating-point format's issues with binary representation and precision. This means that any fractional portion of a number that is slightly less than .5 (because of a loss of precision) will not be rounded upward.
The following example illustrates the problem. It repeatedly adds .1 to 11.0 and rounds the result to the nearest integer. Regardless of the rounding convention, 11.5 should round to 12. However, as the output from the example shows, it does not. The example uses the "R" standard numeric format string to display the floating point value's full precision, and shows that the value to be rounded has lost precision during repeated additions, and its value is actually 11.499999999999998. Because .499999999999998 is less than .5, the value is not rounded to the next highest integer. As the example also shows, this problem does not occur if we simply assign the constant value 11.5 to a Double variable.Copy
using System;
public class Example
{
public static void Main()
{
Console.WriteLine("{0,5} {1,20:R} {2,12} {3,15}\n",
"Value", "Full Precision", "ToEven",
"AwayFromZero");
double value = 11.1;
for (int ctr = 0; ctr <= 5; ctr++)
value = RoundValueAndAdd(value);
Console.WriteLine();
value = 11.5;
RoundValueAndAdd(value);
}
private static double RoundValueAndAdd(double value)
{
Console.WriteLine("{0,5:N1} {0,20:R} {1,12} {2,15}",
value, Math.Round(value, MidpointRounding.ToEven),
Math.Round(value, MidpointRounding.AwayFromZero));
return value + .1;
}
}
// The example displays the following output:
// Value Full Precision ToEven AwayFromZero
//
// 11.1 11.1 11 11
// 11.2 11.2 11 11
// 11.3 11.299999999999999 11 11
// 11.4 11.399999999999999 11 11
// 11.5 11.499999999999998 11 11
// 11.6 11.599999999999998 12 12
//
// 11.5 11.5 12 12
Problems of precision in rounding midpoint values are most likely to arise in the following conditions:
-
When a fractional value cannot be expressed precisely in the floating-point type's binary format.
-
When the value to be rounded is calculated from one or more floating-point operations.
-
When the value to be rounded is a Single rather than a Double or Decimal. For more information, see the next section, Rounding and single-precision floating-point values.
In cases where the lack of precision in rounding operations is problematic, you can do the following:
-
If the rounding operation calls an overload that rounds a Double value, you can change the Double to a Decimal value and call an overload that rounds a Decimal value instead. Although the Decimal data type also has problems of representation and loss of precision, these issues are far less common.
-
Define a custom rounding algorithm that performs a "nearly equal" test to determine whether the value to be rounded is acceptably close to a midpoint value. The following example defines a RoundApproximate method that examines whether a fractional value is sufficiently near to a midpoint value to be subject to midpoint rounding. As the output from the example shows, it corrects the rounding problem shown in the previous example.
using System;
public class Example
{
public static void Main()
{
Console.WriteLine("{0,5} {1,20:R} {2,12} {3,15}\n",
"Value", "Full Precision", "ToEven",
"AwayFromZero");
double value = 11.1;
for (int ctr = 0; ctr <= 5; ctr++)
value = RoundValueAndAdd(value);
Console.WriteLine();
value = 11.5;
RoundValueAndAdd(value);
}
private static double RoundValueAndAdd(double value)
{
const double tolerance = 8e-14;
Console.WriteLine("{0,5:N1} {0,20:R} {1,12} {2,15}",
value,
RoundApproximate(value, 0, tolerance, MidpointRounding.ToEven),
RoundApproximate(value, 0, tolerance, MidpointRounding.AwayFromZero));
return value + .1;
}
private static double RoundApproximate(double dbl, int digits, double margin,
MidpointRounding mode)
{
double fraction = dbl * Math.Pow(10, digits);
double value = Math.Truncate(fraction);
fraction = fraction - value;
if (fraction == 0)
return dbl;
double tolerance = margin * dbl;
// Determine whether this is a midpoint value.
if ((fraction >= .5 - tolerance) & (fraction <= .5 + tolerance)) {
if (mode == MidpointRounding.AwayFromZero)
return (value + 1)/Math.Pow(10, digits);
else
if (value % 2 != 0)
return (value + 1)/Math.Pow(10, digits);
else
return value/Math.Pow(10, digits);
}
// Any remaining fractional value greater than .5 is not a midpoint value.
if (fraction > .5)
return (value + 1)/Math.Pow(10, digits);
else
return value/Math.Pow(10, digits);
}
}
// The example displays the following output:
// Value Full Precision ToEven AwayFromZero
//
// 11.1 11.1 11 11
// 11.2 11.2 11 11
// 11.3 11.299999999999999 11 11
// 11.4 11.399999999999999 11 11
// 11.5 11.499999999999998 12 12
// 11.6 11.599999999999998 12 12
//
// 11.5 11.5 12 12
|