Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to convert floating-point type into integer type in C language?
How to convert floating-point type into integer type in C language?
C language has the following rounding methods:

1, directly assigned to integer variables. For example:

Int i = 2.5 or I = (int) 2.5;

This method uses the decimal part, which can be used in your question.

2. The integer division operator "/"in C/C++has its own integer function (int/int), and the return value of the integer function described below is double. Integer division is the rounding of positive numbers, which can be used in your problem. However, the rounding result of negative numbers by integer division is related to the C compiler used.

3. Use the floor function. Floor(x) returns the largest integer less than or equal to x. For example:

Floor (2.5) = 2

Lower limit (-2.5) = -3

4. Use the ceil function. Ceil(x) returns the smallest integer greater than x. For example:

Upper limit (2.5) = 3

Upper limit (-2.5) = -2

Floor () is rounded to negative infinity, and floor (-2.5) =-3; Ceil () is rounded to positive infinity, and ceil(-2.5) = -2. The floor function can be used to solve your problems.

5、

int b =(int)a; //rounding

int c =(int)(a+0.5); //Round off