The decimal rounding method is as follows:

Source: Internet
Author: User

The decimal rounding method is as follows:

Recently, I found that there was a problem with the code rounded down. I checked the information on the network and recorded the information for future query.

Principle: enlarge the data, and then use the operator's priority to process it.

1. First, you need to know the operation Priority, parentheses → conversion → multiplication and division addition and subtraction.

# Include <stdio. h> long powe (long n); int main (void) {long t; // The amount of transition long I = 3; // the precision of the number of decimal places. Float h = 11.5555; // number t = (long) (h * powe (I + 1) + 5)/10.0); h = (float) t/powe (I); printf ("% f", h); return 0;} long powe (long n) // square {long x = 1; if (n> 0) x = 10 * powe (-- n); elsereturn 1; return x ;}

(Ii) Addition implementation:

# Include <stdio. h> double powe (double n); int main (void) {double x = 11.55556666; // The number of double I = 3 to be rounded down; // The precision is double y = 0; // Result y = 0.01*1.0/powe (I) * (int) (100.0 * powe (I) * (x + 0.005*1.0/powe (I ))); // by default, the first digit after the decimal point is printf ("% f", y); return 0;} double powe (double n) {-- n; double s = 0; if (-- n> 0) s = 10 * powe (-- n); elsereturn 1; return s ;}

Summary:

The preceding two methods are used to round the digits after the decimal point. The first method has the advantage that negative numbers can be used, and the second method cannot. However, in the first method, implicit conversion has a significant impact on the Conversion. Therefore, we recommend that you use the second method.

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.