You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: xml/System/MidpointRounding.xml
+12-9Lines changed: 12 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -55,7 +55,10 @@
55
55
56
56
Use the <xref:System.MidpointRounding> enumeration with appropriate overloads of <xref:System.Math.Round*?displayProperty=nameWithType>, <xref:System.MathF.Round*?displayProperty=nameWithType>, and <xref:System.Decimal.Round*?displayProperty=nameWithType> to provide more control of the rounding process.
57
57
58
-
There are two overall rounding strategies—*round to nearest* and *directed rounding*—and each enumeration field participates in exactly one of these strategies.
58
+
Despite the enumeration's name, not every mode is only about how midpoints are handled. <xref:System.MidpointRounding> defines two overall rounding strategies—*round to nearest* and *directed rounding*—and each enumeration field participates in exactly one of these strategies:
59
+
60
+
- The *round-to-nearest* modes (<xref:System.MidpointRounding.AwayFromZero> and <xref:System.MidpointRounding.ToEven>) always round to the nearest representable value. They only differ from each other when the input is exactly halfway between two values (a *midpoint*), which is what the enumeration's name refers to.
61
+
- The *directed rounding* modes (<xref:System.MidpointRounding.ToNegativeInfinity>, <xref:System.MidpointRounding.ToPositiveInfinity>, and <xref:System.MidpointRounding.ToZero>) always round every value—not just midpoints—in a fixed direction. The result is the nearest representable value in that direction, even when the input isn't a midpoint.
59
62
60
63
## Round to nearest
61
64
@@ -66,9 +69,9 @@ Fields:
66
69
67
70
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.
68
71
69
-
If the next digit is from 0 through 4 or 6 through 9, 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.
72
+
If the next digit is from 0 through 4 or 6 through 9, the choice between <xref:System.MidpointRounding.AwayFromZero> and <xref:System.MidpointRounding.ToEven> doesn't 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 <xref:System.MidpointRounding> enable you to specify whether the rounding operation returns the nearest number away from zero or the nearest even number.
70
73
71
-
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.
74
+
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 <xref:System.MidpointRounding> value to determine the result of rounding. If <xref:System.MidpointRounding.AwayFromZero> is specified, -3 is returned because it is the nearest number away from zero with a precision of zero. If <xref:System.MidpointRounding.ToEven> is specified, -2 is returned because it is the nearest even number with a precision of zero.
A directed-rounding operation takes an original number with an implicit or specified precision and returns the next closest number in a specific direction with the same precision as the original number. Directed modes on `MidpointRounding` control toward which predefined number the rounding is performed.
95
+
A directed-rounding operation takes an original number with an implicit or specified precision and returns the next closest number in a specific direction with the same precision as the original number. Directed modes on <xref:System.MidpointRounding> control the direction toward which every value is rounded. Unlike the round-to-nearest modes, these modes apply to every input value, not just midpoints; for example, <xref:System.MidpointRounding.ToPositiveInfinity> rounds 2.1, 2.5, and 2.8 all up to 3.
93
96
94
97
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.
95
98
@@ -153,7 +156,7 @@ The following table demonstrates the results of rounding some negative and posit
153
156
</ReturnValue>
154
157
<MemberValue>1</MemberValue>
155
158
<Docs>
156
-
<summary>The strategy of rounding to the nearest number, and when a number is halfway between two others, it's rounded toward the nearest number that's away from zero.</summary>
159
+
<summary>The strategy of rounding to the nearest number. When a number is halfway between two others, it's rounded toward the nearest number that's away from zero. Non-midpoint values are unaffected by this choice and are rounded to the nearest representable value.</summary>
157
160
</Docs>
158
161
</Member>
159
162
<MemberMemberName="ToEven">
@@ -193,7 +196,7 @@ The following table demonstrates the results of rounding some negative and posit
193
196
</ReturnValue>
194
197
<MemberValue>0</MemberValue>
195
198
<Docs>
196
-
<summary>The strategy of rounding to the nearest number, and when a number is halfway between two others, it's rounded toward the nearest even number.</summary>
199
+
<summary>The strategy of rounding to the nearest number. When a number is halfway between two others, it's rounded toward the nearest even number. Non-midpoint values are unaffected by this choice and are rounded to the nearest representable value. Also known as banker's rounding.</summary>
197
200
</Docs>
198
201
</Member>
199
202
<MemberMemberName="ToNegativeInfinity">
@@ -222,7 +225,7 @@ The following table demonstrates the results of rounding some negative and posit
222
225
</ReturnValue>
223
226
<MemberValue>3</MemberValue>
224
227
<Docs>
225
-
<summary>The strategy of downwards-directed rounding, with the result closest to and no greater than the infinitely precise result.</summary>
228
+
<summary>The strategy of downwards-directed rounding, where every value—not just midpoints—is rounded toward negative infinity. The result is the value closest to and no greater than the infinitely precise result. When rounding to zero digits, this is equivalent to the mathematical floor function; when rounding to a non-zero number of digits, it applies the same behavior at the specified precision.</summary>
226
229
</Docs>
227
230
</Member>
228
231
<MemberMemberName="ToPositiveInfinity">
@@ -251,7 +254,7 @@ The following table demonstrates the results of rounding some negative and posit
251
254
</ReturnValue>
252
255
<MemberValue>4</MemberValue>
253
256
<Docs>
254
-
<summary>The strategy of upwards-directed rounding, with the result closest to and no less than the infinitely precise result.</summary>
257
+
<summary>The strategy of upwards-directed rounding, where every value—not just midpoints—is rounded toward positive infinity. The result is the value closest to and no less than the infinitely precise result. When rounding to zero digits, this is equivalent to the mathematical ceiling function; when rounding to a non-zero number of digits, it applies the same behavior at the specified precision.</summary>
255
258
</Docs>
256
259
</Member>
257
260
<MemberMemberName="ToZero">
@@ -280,7 +283,7 @@ The following table demonstrates the results of rounding some negative and posit
280
283
</ReturnValue>
281
284
<MemberValue>2</MemberValue>
282
285
<Docs>
283
-
<summary>The strategy of directed rounding toward zero, with the result closest to and no greater in magnitude than the infinitely precise result.</summary>
286
+
<summary>The strategy of directed rounding toward zero, where every value—not just midpoints—is rounded toward zero. The result is the value closest to and no greater in magnitude than the infinitely precise result. When rounding to zero digits, this is equivalent to truncation; when rounding to a non-zero number of digits, it truncates toward zero at the specified precision.</summary>
0 commit comments