四舍五入到小数点后 4 位 - 意外结果
Rounding to 4 decimal places - Unexpected results
四舍五入时我得到以下输出:
Math.Round(2.3234503,4) = 2.3235
Math.Round(2.323450,4) = 2.3234
我不明白这是怎么回事,有人能给我指出来吗?我预计两次的答案都是 2.3235。数字 3 或小数点后一位缺失如何影响结果?
来自MSDN
Returns:
The number nearest to d that contains a number of fractional digits
equal to decimals.
关键字最近。
2.3234 503 - 最接近 503 的是 5,因此四舍五入为 5
2.3234 50 - 已过半。
在 MSDN
的中途
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.
By default, the Round method uses the rounding to nearest convention.
四舍五入时我得到以下输出:
Math.Round(2.3234503,4) = 2.3235
Math.Round(2.323450,4) = 2.3234
我不明白这是怎么回事,有人能给我指出来吗?我预计两次的答案都是 2.3235。数字 3 或小数点后一位缺失如何影响结果?
来自MSDN
Returns:
The number nearest to d that contains a number of fractional digits equal to decimals.
关键字最近。
2.3234 503 - 最接近 503 的是 5,因此四舍五入为 5
2.3234 50 - 已过半。
在 MSDN
的中途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.
By default, the Round method uses the rounding to nearest convention.