Floating-point rounding and banker rounding for floating-point reshaping in C # to implement code _c# tutorial

Source: Internet
Author: User
Double display convert int
Copy Code code as follows:

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 you can see that the floating-point type display is converted to an integer to remove the decimal, and only the integral part.
This is explained on MSDN: when you use an explicit conversion to perform the same transformation in C #, the value to the right of the decimal point is lost.
Here we tried double and float type, and the result is the same as above!
Is the rounding wrong? Convert.ToInt32
Copy Code code as follows:

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 found that 2.5,-18.5 did not comply with the rules of our early learning rounding! But 5.5 was correctly converted to 6. 5.1,2.8 these are all normal according to four
The law of five.
Banker's Rounding Act

MSDN Under Convert.ToInt32 method
public static int ToInt32 (decimal value);
public static int ToInt32 (double value);
public static int ToInt32 (float value);
MSDN has a special explanation for the return of these methods:
Rounded to the value of the nearest 32-bit signed integer. If the value is a number in the middle of two integers, the even numbers in the two are returned, that is, 4.5 is converted to 4, and 5.5 is converted to 6.
Refer to the relevant information to conclude that this rounding rule is called Banker's rounding Method:
Banker rounding is one of the decimal rounding standards set by the IEEE and the best rounding method in the current IEEE regulations, so all languages conforming to the IEEE standard should implement this algorithm. NET platform is no exception.

The rounding rule is:
A decimal, when the place is less than 5, then give up this.

When the place of the shed is equal to 5, then go to see the number of the first bit of parity, if it is odd, then give up 5, and then put the front of a bit plus 1, on the contrary: if it is even, then give up to 5, leaving the bit to retain even the nature of the same.

When the place is more than 5, then give up a bit do not, give up the front of a plus 1.

This rule has the same effect on negative numbers!
Example:
4.3==4
4.5==4
5.5==6
6.5==6

Implementation of banker rounding method in net
Copy Code code as follows:

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"));
}

Looking at the code above, you can find the compound banker rounding method from several places
Copy Code code as follows:

int num = (int) value;
Double num2 = value-num;
if ((num2 > 0.5) | | ((num2 = = 0.5) && ((num & 1)!= 0))
{
num++;
}
return num;

The analysis can be found by first showing the conversion of value minus decimal to num, and then the decimal difference between value and num, and then according to the banker's rules.
((num2 = = 0.5) && ((num & 1)!= 0))
If the shed is equal to 0.5, and through the bitwise operation is an odd number, if 2 conditions are consistent, then + 1. Otherwise, NUM is returned.
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.