Rounding to Nearest Int, Tenth, Hundredth, Thousandth

#include<iostream>
#include<cmath>


using namespace std;
float num;

float roundToInteger(float x)
{
     return floor( x + .5);
    
}

float roundToTenths(float x)
{
    x /=10;
    return floor(x + 0.5) * 10;
}
    
float roundToHundredths(float x){
      x /=100;
       return floor(x + 0.5) * 100;
}

float roundToThousandths(float x)
{
     x /=1000;
       return floor(x + 0.5) * 1000;  
}

main(){
       cout<<"enter a number: ";
       cin>>num;
       cout<<"\nroundToInteger: " << roundToInteger(num)<<endl;
       cout<<"\nroundToTenths: " << roundToTenths(num)<<endl;
       cout<<"\nroundToHundredths: " << roundToHundredths(num)<<endl;
       cout<<"\nroundToThounsandths: " << roundToThousandths(num)<<endl;
       
system("pause");
  
}

Advertisements

About praveenaj

I'm just an average guy who enjoys doing Programming/Web Designs/Pencil art & drinking coffee :)
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s