Skip to content

Improving midpoint rounding documentation #4485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@ public static void Main()
{
//<snippet1>
decimal result = 0.0m;
decimal posValue = 3.45m;
decimal negValue = -3.45m;
decimal positiveValue = 3.45m;
decimal negativeValue = -3.45m;

// By default, round a positive and a negative value to the nearest even number.
// The precision of the result is 1 decimal place.

result = Math.Round(posValue, 1);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue);
result = Math.Round(negValue, 1);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue);
result = Math.Round(positiveValue, 1);
Console.WriteLine($"{result} = Math.Round({positiveValue}, 1)");
result = Math.Round(negativeValue, 1);
Console.WriteLine($"{result} = Math.Round({negativeValue}, 1)");
Console.WriteLine();

// Round a positive value to the nearest even number, then to the nearest number away from zero.
// The precision of the result is 1 decimal place.

result = Math.Round(posValue, 1, MidpointRounding.ToEven);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, posValue);
result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, posValue);
result = Math.Round(positiveValue, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({positiveValue}, 1, MidpointRounding.ToEven)");
result = Math.Round(positiveValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({positiveValue}, 1, MidpointRounding.AwayFromZero)");
Console.WriteLine();

// Round a negative value to the nearest even number, then to the nearest number away from zero.
// The precision of the result is 1 decimal place.

result = Math.Round(negValue, 1, MidpointRounding.ToEven);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, negValue);
result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, negValue);
result = Math.Round(negativeValue, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({negativeValue}, 1, MidpointRounding.ToEven)");
result = Math.Round(negativeValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({negativeValue}, 1, MidpointRounding.AwayFromZero)");
Console.WriteLine();
/*
This code example produces the following results:

3.4 = Math.Round( 3.45, 1)
3.4 = Math.Round(3.45, 1)
-3.4 = Math.Round(-3.45, 1)

3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven)
3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero)
3.4 = Math.Round(3.45, 1, MidpointRounding.ToEven)
3.5 = Math.Round(3.45, 1, MidpointRounding.AwayFromZero)

-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)
Expand Down
37 changes: 25 additions & 12 deletions xml/System/MidpointRounding.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@
<summary>Specifies how mathematical rounding methods should process a number that is midway between two numbers.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
Use `MidpointRounding` with appropriate overloads of <xref:System.Math.Round%2A?displayProperty=nameWithType> to provide more control of the rounding process.

### Round to nearest
A round-to-nearest operation takes an original number with an implicit or specified precision; examines the next digit, which is at that precision plus one; and returns the nearest number with the same precision as the original number. For positive numbers, if the next digit is from 0 through 4, the nearest number is toward negative infinity. If the next digit is from 6 through 9, the nearest number is toward positive infinity. For negative numbers, if the next digit is from 0 through 4, the nearest number is toward positive infinity. If the next digit is from 6 through 9, the nearest number is toward negative infinity.

## Remarks
Use `MidpointRounding` with appropriate overloads of <xref:System.Math.Round%2A?displayProperty=nameWithType> to provide more control of the rounding process.
In the previous cases, the `MidpointRounding.AwayFromZero` and `MidpointRounding.ToEven` do not affect the result of the rounding operation. However, if the next digit is 5, which is the midpoint between two possible results, and all remaining digits are zero or there are no remaining digits, the nearest number is ambiguous. In this case, the round-to-nearest modes in `MidpointRounding` enable you to specify whether the rounding operation returns the nearest number away from zero or the nearest even number.

A rounding operation takes an original number with an implicit or specified precision; examines the next digit, which is at that precision plus one; and returns the nearest number with the same precision as the original number. For positive numbers, if the next digit is from 0 through 4, the nearest number is toward negative infinity. If the next digit is from 6 through 9, the nearest number is toward positive infinity. For negative numbers, if the next digit is from 0 through 4, the nearest number is toward positive infinity. If the next digit is from 6 through 9, the nearest number is toward negative infinity.

In the previous cases, the `MidpointRounding` enumeration does not affect the result of the rounding operation. However, if the next digit is 5, which is the midpoint between two possible results, and all remaining digits are zero or there are no remaining digits, the nearest number is ambiguous. In this case, the `MidpointRounding` enumeration enables you to specify whether the rounding operation returns the nearest number away from zero or the nearest even number.

The following table demonstrates the results of rounding some negative and positive numbers in conjunction with the values of `MidpointRounding`. The precision used to round the numbers is zero, which means the number after the decimal point affects the rounding operation. For example, for the number -2.5, the digit after the decimal point is 5. Because that digit is the midpoint, you can use a `MidpointRounding` value to determine the result of rounding. If `AwayFromZero` is specified, -3 is returned because it is the nearest number away from zero with a precision of zero. If `ToEven` is specified, -2 is returned because it is the nearest even number with a precision of zero.
The following table demonstrates the results of rounding some negative and positive numbers in conjunction with round-to-nearest modes. The precision used to round the numbers is zero, which means the number after the decimal point affects the rounding operation. For example, for the number -2.5, the digit after the decimal point is 5. Because that digit is the midpoint, you can use a `MidpointRounding` value to determine the result of rounding. If `AwayFromZero` is specified, -3 is returned because it is the nearest number away from zero with a precision of zero. If `ToEven` is specified, -2 is returned because it is the nearest even number with a precision of zero.

|Original number|AwayFromZero|ToEven|
|---------------------|------------------|------------|
Expand All @@ -73,6 +73,19 @@
|-2.8|-3|-3|
|-3.5|-4|-4|

## Directed rounding
A directed rounding operation takes an original number with an implicit or specified precision and returns the next number closest to some predefined one with the same precision as the original number. Directed modes on `MidpointRounding` control toward which predefined number the rounding is performed.

The following table demonstrates the results of rounding some negative and positive numbers in conjunction with directed rounding modes. The precision used to round the numbers is zero, which means the number before the decimal point is affected by the rounding operation.

|Original number|ToNegativeInfinity|ToPositiveInfinity|ToZero|
|---------------------|------------------|------------|------|
|2.8|2|3|2|
|2.5|2|3|2|
|2.1|2|3|2|
|-2.1|-3|-2|-2|
|-2.5|-3|-2|-2|
|-2.8|-3|-2|-2|


## Examples
Expand Down Expand Up @@ -126,7 +139,7 @@
</ReturnValue>
<MemberValue>1</MemberValue>
<Docs>
<summary>When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.</summary>
<summary>Round to nearest mode: when a number is halfway between two others, it is rounded toward the nearest number that is away from zero.</summary>
</Docs>
</Member>
<Member MemberName="ToEven">
Expand Down Expand Up @@ -169,7 +182,7 @@
</ReturnValue>
<MemberValue>0</MemberValue>
<Docs>
<summary>When a number is halfway between two others, it is rounded toward the nearest even number.</summary>
<summary>Round to nearest mode: when a number is halfway between two others, it is rounded toward the nearest even number.</summary>
</Docs>
</Member>
<Member MemberName="ToNegativeInfinity">
Expand Down Expand Up @@ -203,7 +216,7 @@
</ReturnValue>
<MemberValue>3</MemberValue>
<Docs>
<summary>When a number is halfway between two others, it is rounded toward the result closest to and no greater than the infinitely precise result.</summary>
<summary>Directed mode: the number is rounded down, with the result closest to and no greater than the infinitely precise result.</summary>
</Docs>
</Member>
<Member MemberName="ToPositiveInfinity">
Expand Down Expand Up @@ -237,7 +250,7 @@
</ReturnValue>
<MemberValue>4</MemberValue>
<Docs>
<summary>When a number is halfway between two others, it is rounded toward the result closest to and no less than the infinitely precise result.</summary>
<summary>Directed mode: the number is rounded up, with the result closest to and no less than the infinitely precise result.</summary>
</Docs>
</Member>
<Member MemberName="ToZero">
Expand Down Expand Up @@ -271,7 +284,7 @@
</ReturnValue>
<MemberValue>2</MemberValue>
<Docs>
<summary>When a number is halfway between two others, it is rounded toward the result closest to and no greater in magnitude than the infinitely precise result.</summary>
<summary>Directed mode: the number is rounded toward zero, with the result closest to and no greater in magnitude than the infinitely precise result.</summary>
</Docs>
</Member>
</Members>
Expand Down