/* --------------------------------------------------------------------- // Figure 7.20: How to round. // Figure 7.21: Rounding and casting. // --------------------------------------------------------------------- */ #include "tools.h" void main( void ) { double x, y = 17.7, z = -17.7; int k, m, n; m = round(y); /* Round y to nearest integer. */ n = round(z); /* Round z to nearest integer. */ printf( "Rounding: y= %6.2f m= %3i \n", y, m ); printf( "Rounding: z= %6.2f n= %3i \n", z, n ); k = (int) y; /* Casting float to int truncates. */ x = (double) k; /* Casting back does not restore the fractional part. */ printf( "Casting: y= %6.2f k= %3i x= %6.2f \n", y, k, x ); }