Convert.Toint32(double value) rounding to 50% double rule

When converting a decimal to an integer with Convert.Toint32(), it will be rounded. for example:

Convert.Toint32(4.1) = 4
Convert.Toint32(4.3) = 4
Convert.Toint32(4.51) = 5
Convert.Toint32(4.6) = 5

However, if the decimal is followed by an intermediate number such as .5 or .50, the converted result will be unexpected. In short, the result is:
if value is a number between two integers, return the even number of the two.
For example Convert.Toint32(3.5), 3.5 is a number between the integers 3 and 4, and the even number 4 is taken at this time.
For example Convert.Toint32(4.5), 4.5 is a number between the integers 4 and 5, and the even number 4 is taken at this time

Look at Convert.Toint32()the official specific implementation:

public static int ToInt32(double value)
{
    if (value >= 0)
    {
        if (value < 2147483647.5)
        {
            int result = (int)value;
            double dif = value - result;
            if (dif > 0.5 || dif == 0.5 && (result & 1) != 0) result++;
            return result;
        }
    }
    else
    {
        if (value >= -2147483648.5)
        {
            int result = (int)value;
            double dif = value - result;
            if (dif < -0.5 || dif == -0.5 && (result & 1) != 0) result--;
            return result;
        }
    }
    throw new OverflowException(SR.Overflow_Int32);
}

From here we see the if (dif > 0.5 || dif == 0.5 && (result & 1) != 0) result++;two if (dif < -0.5 || dif == -0.5 && (result & 1) != 0) result--;lines of code, where the rounding is greater than 0.5, but it is equal to 0.5 and it must be judged whether it is an odd number, and it will be carried if it is an odd number.

This is called odd and even rounding, also known as the rounding rule, Banker's Rounding, which is a counting retention method and a number rounding rule. From a statistical point of view, "odd and even rounding" is more accurate than "rounding": in a large number of calculations, because some of the rounded results become larger and some become smaller, the mean error of the rounded results is even greater. tends to zero. Instead of rounding every fifth, the result is biased towards large numbers, causing errors to accumulate and resulting in systematic errors. "Odd and even rounding" minimizes the effect of rounding errors on the measurement results. The specific requirements are as follows (taking two decimal places as an example):
(1) If the last digit required to be reserved is 4, it will be rounded down. For example, 5.214 with two decimal places is 5.21.
(2) If the last digit of the reserved digit is 6, enter it. For example, 5.216 with two decimal places is 5.22.
(3) If the last digit of the reserved digit is 5, and there are no more numbers after 5, it is necessary to decide whether to discard or enter according to the previous digit of the mantissa "5": if it is an odd number, enter it; if it is an even number then give up. For example, 5.215 with two decimal places is 5.22; 5.225 with two decimal places is 5.22.
(4) If the last digit of the reserved digit is 5, and there are still numbers after 5. For example, 5.2254 retains two decimal places as 5.23, that is to say, if there is data after 5, it must be entered regardless of odd or even.

Original text:Convert.Toint32(double value) rounding up to 50% double rule - Dong Chuanmin

Guess you like

Origin blog.csdn.net/weixin_42565127/article/details/130925083