ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

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

Updated on December 11, 2010

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.
Output of the sample code.

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

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Bob 7 years ago

      useless article

    • nicomp profile image
      Author

      nicomp really 7 years ago from Ohio, USA

      @Bob: Ouch! I worked hard on it.

    • profile image

      Suti 7 years ago

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

    • nicomp profile image
      Author

      nicomp really 7 years ago from Ohio, USA

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

    • profile image

      Amrita 6 years ago

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

    Click to Rate This Article