# Examples of rounding and truncating floating point values in C Plus Plus

C++ (C Plus Plus) provides built-in functions for truncating and rounding floating point data items. The ceil function calculates the next highest integer value for a given floating point value. The floor function calculates the next lowest integer value for a given floating point number.

ceil(x) returns the smallest integer not less than x.

floor(x) returns the largest integer not greater than x.

if x is a whole number, then ceil(x) and floor(x) both return x.

For example...

ceil(1.1) returns the integer 2.

floor(1.1) returns the integer 1.

The example code, below,  is targeted for Microsoft C++ in Microsoft Visual Studio.

## Examples of rounding and truncating floating point values in C Plus Plus

```//****************************************************************************************
//                                                                    ceil and floor.cpp *
// Examples of rounding and truncating floating point values in C++                      *
// Author: nicomp                                                                        *
//                                                                                       *
//****************************************************************************************
#include <iostream>
#include <math.h>
using namespace std;
void main()
{
double rounded = 0;
double num = .5;
cout << "\n ceil(" << num << ")  " << ceil(num);
cout << "\n floor(" << num << ")  " << floor(num);
if (num - floor(num) >= .5) rounded = floor(num + 1); else rounded = floor(num);
cout << "\n " << num << " rounds to " << rounded;

num = .4999;
cout << "\n ceil(" << num << ")  " << ceil(num);
cout << "\n floor(" << num << ")  " << floor(num);
if (num - floor(num) >= .5) rounded = floor(num + 1); else rounded = floor(num);
cout << "\n " << num << " rounds to " << rounded;

num = .50001;
cout << "\n ceil(" << num << ")  " << ceil(num);
cout << "\n floor(" << num << ")  " << floor(num);
if (num - floor(num) >= .5) rounded = floor(num + 1); else rounded = floor(num);
cout << "\n " << num << " rounds to " << rounded;

num = .51;
cout << "\n ceil(" << num << ")  " << ceil(num);
cout << "\n floor(" << num << ")  " << floor(num);
if (num - floor(num) >= .5) rounded = floor(num + 1); else rounded = floor(num);
cout << "\n " << num << " rounds to " << rounded;

num = .99;
cout << "\n ceil(" << num << ")  " << ceil(num);
cout << "\n floor(" << num << ")  " << floor(num);
if (num - floor(num) >= .5) rounded = floor(num + 1); else rounded = floor(num);
cout << "\n " << num << " rounds to " << rounded << "\n";
}
```
Output of the sample code.

## More by this Author

Bob 7 years ago

useless article

nicomp 7 years ago from Ohio, USA Author

@Bob: Ouch! I worked hard on it.

Suti 7 years ago

nicomp: it's a nice article, but not what i'm looking for :)

nicomp 7 years ago from Ohio, USA Author

@Suti: Thanks for writing. Let me know what you're looking for and I'll write it up for you.

Amrita 5 years ago

nice article..i found it useful..thanks :) :)