Rounding Numbers (2/28/11)

#include


#include

#include

float roundtotenths(float number);

float roundtohundreths(float number);

float roundtothousandths(float number);

float roundtointeger(float number);

//------------------------------------

//------------------------------------

int main(void)

{

float n;

float result1;

float result2;

float result3;

float result4;

printf("Enter a number:\n");

scanf("%f",&n);

result1= roundtotenths(n);

printf("Rounded to tenths version of %f is %f.\n\n",n,result1);

//------------------------------------

result2= roundtohundreths(n);

printf("Rounded to hundreths version of %f is %f.\n\n",n,result2);

//------------------------------------

result3= roundtothousandths(n);

printf("Rounded to thousandths version of %f is %f.\n\n",n,result3);

//------------------------------------

result4= roundtointeger(n);

printf("Rounded to integer version of %f is %f.\n\n",n,result4);

//------------------------------------

getch();

}

//------------------------------------

//------------------------------------

float roundtotenths(float number)

{

float y;

y=floor(number*10+.5)/10;

return y;

}

//------------------------------------

//------------------------------------

float roundtohundreths(float number)

{

float x;

x=floor(number*100+.5)/100;

return x;

}

//------------------------------------

//------------------------------------

float roundtothousandths(float number)

{

float z;

z=floor(number*1000+.5)/1000;

return z;

}

//------------------------------------

//------------------------------------

float roundtointeger(float number)

{

float j;

j=floor(number+.5);

return j;

}

//------------------------------------

//-----------------------------------



Output:

Enter a number:


2.5333333

Rounded to tenths version of 2.533333 is 2.500000.

Rounded to hundreths version of 2.533333 is 2.530000.

Rounded to thousandths version of 2.533333 is 2.533000.

Rounded to integer version of 2.533333 is 3.000000.