Skip to main content
Logo image

Section 18.9 Rounding in C

C supports several functions to help with rounding (such as rounding up, rounding down, just rounding, etc). You need to
#include <math.h>
in order to use them. Here are some of the functions at your disposal:
 
Table 18.1. Rounding Functions in C
Function Prototype Description
floor() double floor(double x);
Returns the nearest integer which is less than or equal to the argument passed to this function.
round() double round (double x);
Returns the nearest integer value of the argument passed to this function.
lround() long lround (double x);
Returns the nearest integer value of the argument passed to this function.
ceil() double ceil (double x);
Returns the nearest integer value which is greater than or equal to the argument passed to this function.
 

Activity 18.4.

Why might it be useful to have both a round() and an lround() function?