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.