C # simple and easy-to-use Rounding Method

Source: Internet
Author: User

Rounding is a problem that occurs frequently in software development. I have used this operation in more than one project. If you search for c # On the internet, there will be many results, however, most of the content is about Microsoft's Math. round () is inaccurate and belongs to "four homes, six homes, six homes, and five into two". Then many people write their own methods to implement this. Some of them are very simple, some are very complicated, and some are not;

In fact, it is really not necessary to implement this function; since we know that c # has Math. round (), didn't we find out how many eight overloaded methods it has? One of them is the parameter of the MidpointRounding type, as long as we make good use of it, it is completely possible to achieve a very accurate rounding, there is no need to write such a complex method.


MidpointRounding: specifies how the mathematical rounding method processes the median between two numbers.


The following table shows the rounding results of some positive and negative numbers used in combination with the value of the MidpointRounding. The precision used when rounding a number is zero, which means that the number after the decimal point will affect the rounding operation. For example, for the number-2.5, the number after the decimal point is 5. Because this number is an intermediate value, you can use the MidpointRounding value to determine the rounding result. If AwayFromZero is specified,-3 is returned because it is a number with zero precision and closest to zero. If ToEven is specified,-2 is returned because it is the nearest even number with zero precision.

Original number
Take a value with a large absolute value
Returns an even number.
3.5
4
4
2.8
3
3
2.5
3
2
2.1
2
2
-2.1
-2
-2
-2.5
-3
-2
-2.8
-3
-3
-3.5
-4
-4


I don't need to explain it any more. If you want to round 3.455 to 3.46, you only need to use the following code.

 
====================================

Math. Round (3.455, 2, MidpointRounding. AwayFromZero)

====================================

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.