Integer processing methods, such as integer rounding and decimal retention

Source: Internet
Author: User

Integer processing methods, such as integer rounding and decimal retention

Problem description:

The background data type is decimal a, which must be an integer when displayed on the foreground page.

My practice is Convert. ToInt32 (a). In this way, the content is forcibly converted to an integer, changing the original meaning of the data.

Math. Round (a, 0) is used to Round the small value of a to the specified precision.

1. first look at the Math. Round () method to Round the value to the nearest integer or the specified decimal place.

On MSDN, you can see these methods for overloading.

Math. Round (3.44, 1); // Returns 3.4.Math.Round (3.45, 1); // Returns 3.4.Math.Round (3.46, 1); // Returns 3.5. The example given on MSDN
Math. Round (3.55, 1); // Returns 3.6 An example I added

We can see that this "Rounding" is not rounding, but the banker Rounding: four homes, six in five get even method. The rule is that when the value of a given round is smaller than 5, it is directly rounded up. When the value of a given round is greater than or equal to 6, it is placed forward when it is rounded up; when the value of the deprecated digit is equal to 5, if the value of the forward digit is odd, the value is rounded up to the forward digit. If the value of the forward digit is even, the value is directly rounded up.

2. The Convert. ToInt32 (value d) method is a type conversion method.

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, which is also a banker's rounding.

Conclusion: decimal is rounded up. The difference between decimal and decimal is that the decimal point is rounded to the specified precision without changing the data type. The latter is forced to convert to change the data type.

We mentioned the difference between the (int) method and Convert. ToInt32 () method in forced type conversion to an integer:

The (int) method directly intercepts the integer part, while Convert. ToInt32 () adopts the banner rounding method.

There is also the int. Parse (string s) method. The parameter must be of the string type, and the string type parameter should also be of the int type in actual sense,

 

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.