) Math. Round ()-C # differences with Java

Source: Internet
Author: User

Bytes

 

 

It is clearly known that Java and C # are different in many aspects, but it cannot be described in detail.

When I encountered a problem some time ago, I suddenly realized a different one.

Some students have written a test on math. Round (11.5) and math. Round (-11.5.

A colleague is from Java and replied directly, isn't it 12 or-11.

Another colleague is engaged in. NET and wroteProgramThe result is 12 and-12.

Why? Why are the results different?

I checked the Java help document and the msdn help document respectively, and found that the original was caused by different standards.

Java's round is rounded up to + 0.5. Therefore, the result is of course 12 and-11.

But. NET is different, and the description is also responsible for many. In its remarks, there is such a description:

This method follows section 754 of IEEE Standard 4th. Such rounding is sometimes called proximity rounding or banker rounding.

Oh, so far, I finally understood why it was different. But what is banner rounding?

After searching for half a day, I found the explanation:

For the 10 digits 0-9, the probability of appearing in a certain digit is equal. In financial settlement, if you simply use rounding, the incoming parameter (5-9) will be one more than the value (0-4), that is, the financial institution will lose money, in this case, the nearest round is also taken into consideration to make rounding more equitable. Since each digit has the same 0-9 probability, the probability of an even or odd number is equal. There is no doubt about dropping a digit, 0-4, and 6-9, it mainly lies in the processing of the number 5 of the pairs. At this time, decide whether or not to discard the decision by the former digit. If the former digit is an even, discard it. If the former digit is an odd one, carry it, it achieves roughly fair rounding.

Round by banker, consider 11.5 and-11.5. For 11.5, the front side of 5 is an odd number of 1, so carry, the result is 12; For-11.5, it is changed to-12. Therefore, this round-robin method is called the four homes and six in five directions.

It turns out that.

 

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.