Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - About C language: Round a double-precision data.
About C language: Round a double-precision data.
When double is converted to integer, you can round the double data type by using forced rounding.

1 Forces the conversion of data of type double to type int, which will force the truncation of the integer part.

For example, double a =1.23; The value of (int)a is 1.

Because it is forced rounding, even after 1.9 is converted into int, 1 does not meet the rounding requirements. So you can change the algorithm and do (int)(a+0.5) to achieve the effect of rounding to an integer.

To round to a certain position, you can multiply by a value, move the bit to a position, round up, divide by this value, and then move back.

For example, round the double a = 1.2345 to two decimal places, and you can write.

(int)(a * 100+0.5)/ 100.0 .