Warning
Although ceil() and floor() convert their arguments to integers, each function
returns a float value. That’s why the dollars variable was printed using the %f
conversion code.
The ceil() function (which is the opposite of floor()) finds the next-highest integer. Both
ceil() and floor() work with negative values, too, as the following few lines show:
Click here to view code image
lowVal1 = floor(18.5); // Stores 18.0
lowVal2 = floor(-18.5); // Stores -19.0
hiVal1 = ceil(18.5); // Stores 19.0
hiVal2 = ceil(-18.5); // Stores =18.0
Note
The negative values make sense when you think about the direction of negative
numbers. The next integer down from –18.5 is –19. The next integer up from –18.5 is –
18.
See, these functions aren’t so bad, and they come in handy when you need them.
Doing More Conversions
Two other numeric functions convert numbers to other values. The fabs() function returns the
floating-point absolute value. When you first hear about absolute value, it sounds like something
you’ll never need. The absolute value of a number, whether it is negative or positive, is the positive
version of the number. Both of these printf() functions print 25 :
Click here to view code image
printf("Absolute value of 25.0 is %.0f.\n", fabs(25.0));
printf("Absolute value of -25.0 is %.0f.\n", fabs(-25.0));
Note
The floating-point answers print without decimal places because of the .0 inside the
%f conversion codes.
Absolute values are useful for computing differences in ages, weights, and distances. For example,
the difference between two people’s ages is always a positive number, no matter how you subtract
one from the other.