Rounding of floating point to integer in c #-rounding and banker rounding

Source: Internet
Author: User

Double display conversion int
 
Static void Main (string [] args)
{
Console. WriteLine ("5.1 ~ {0} ", (int) 5.1d );
Console. WriteLine ("5.5 ~ {0} ", (int) 5.5d );
Console. WriteLine ("5.8 ~ {0} ", (int) 5.8d );
Console. WriteLine ("2.1 ~ {0} ", (int) 2.1d );
Console. WriteLine ("2.5 ~ {0} ", (int) 2.5d );
Console. WriteLine ("2.8 ~ {0} ", (int) 2.8d );
Console. WriteLine ("-18.2 ~ {0} ", (int)-18.2f );
Console. WriteLine ("-18.5 ~ {0} ", (int)-18.5f );
Console. WriteLine ("-18.9 ~ {0} ", (int)-18.9f );
Console. Read ();
}




 

Here we can see that the floating point type display is converted to an integer to remove the following decimal places, only take the integer part.
As explained in MSDN: when the same conversion is performed in C # using an explicit conversion, the value on the right of the decimal point will be lost.
Here we tried the double and float types, and the results are the same as above!
 
 
Is it wrong to rounding it down? Convert. ToInt32

 
Static void Main (string [] args)
{
Console. WriteLine ("5.1 ~ {0} ", Convert. ToInt32 (5.1d ));
Console. WriteLine ("5.5 ~ {0} ", Convert. ToInt32 (5.5d ));
Console. WriteLine ("5.8 ~ {0} ", Convert. ToInt32 (5.8d ));
Console. WriteLine ("2.1 ~ {0} ", Convert. ToInt32 (2.1d ));
Console. WriteLine ("2.5 ~ {0} ", Convert. ToInt32 (2.5d ));
Console. WriteLine ("2.8 ~ {0} ", Convert. ToInt32 (2.8d ));
Console. WriteLine ("-18.2 ~ {0} ", Convert. ToInt32 (-18.2f ));
Console. WriteLine ("-18.5 ~ {0} ", Convert. ToInt32 (-18.5f ));
Console. WriteLine ("-18.9 ~ {0} ", Convert. ToInt32 (-18.9f ));
Console. Read ();
}



 

From the above results, we found that 2.5,-18.5 did not follow the rules we learned from childhood! But 5.5 is correctly converted to 6. 5.1 and 2.8 are normal.
Five rules.
 
 
Bankers rounding
 

Convert. ToInt32 method under MSDN
Public static int ToInt32 (decimal value );
Public static int ToInt32 (double value );
Public static int ToInt32 (float value );
 
Msdn provides a special explanation of the returned results of these methods:
Round to the value of the nearest 32-bit signed integer. If value is a number in the middle of two integers, an even number is returned. That is, 4.5 is converted to 4, and 5.5 is converted to 6.
 
Check the relevant information and obtain the Rounding Rule called the banker's rounding method:
Banker rounding is one of the IEEE-defined fractional rounding standards and is also the best Rounding Method in IEEE's current regulations. Therefore, all IEEE-compliant languages should implement this algorithm ,. NET platform is no exception. Www.2cto.com
 
 
The rounding rule is:
A decimal number. If the number of decimal places is less than 5, the number of decimal places is exceeded.
 
 
When the rounding is equal to 5, let's look at the parity of the first digit. If it is an odd number, we will discard 5 and then add 1 to the first digit. On the contrary: if it is an even number, 5 is removed, and the even number is retained.
 
 
When the number of places to be removed is greater than 5, the number of places to be removed is not. Add 1 to the first place.
 
 
This rule also applies to negative numbers!
 
Example:
4.3 = 4
4.5 = 4
5.5 = 6
6.5 = 6
 
 
NET Implementation of the bankers Rounding Method

 
Public static int ToInt32 (double value)
{
If (value >=0.0)
{
If (value <2147483647.5)
{
Int num = (int) value;
Double num2 = value-num;
If (num2> 0.5) | (num2 = 0.5) & (num & 1 )! = 0 )))
{
Num ++;
}
Return num;
}
}
Else if (value >=- 2147483648.5)
{
Int num3 = (int) value;
Double num4 = value-num3;
If (num4 <-0.5) | (num4 =-0.5) & (num3 & 1 )! = 0 )))
{
Num3 --;
}
Return num3;
}
Throw new OverflowException (Environment. GetResourceString ("Overflow_Int32 "));
}


View the code above and find the compound banker rounding method from several places
 
Int num = (int) value;
Double num2 = value-num;
If (num2> 0.5) | (num2 = 0.5) & (num & 1 )! = 0 )))
{
Num ++;
}
Return num;
 
The analysis shows that the result is to convert the decimal number of the value to num, then obtain the decimal difference between the value and num, and then remove it according to the banker's law.
(Num2 = 0.5) & (num & 1 )! = 0 ))
If the round-robin is equal to 0.5 and the bitwise operation is performed to determine whether the number is an odd number, if both conditions are met, then + 1 is returned. Otherwise, return num.

 

From the sea, not blue

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.