How do I round decimals

• 03-19-2002
fastlane29
How do I round decimals
I have a program to write for my cs115 class that I have to find a hat size in a function and after I compute it I must return only 4 sizes(x.00,x.25,x.50,x.75) for example if I come up with 6.45 as the size I must round it up to 6.50 or if it is 6.60 then I must round it up to 6.75. here is my function:

float size(float weight,float height)
{
return((weight/height)*2.9);
}

Any help will be greatly appreciated my program is due and this is the only thing of it that I cannot figure out.

Fastlane29
• 03-19-2002
biosx
You could just use printf() to print out the rounded decimal.
Code:

```#include <stdio.h> int main() {   double dnum = 6.78;   printf("%3.1f", dnum); // Will print out 6.8   return 0; }```
Is that acceptable? :)
• 03-19-2002
Prelude
Code:

```double round ( double value, unsigned decimal ) {   return ceil ( value * pow ( 10.0, decimal ) + .5 ) / pow ( 10.0, decimal ); }```
-Prelude
• 03-19-2002
biosx
Quote:

Originally posted by Prelude
Code:

```double round ( double value, unsigned decimal ) {   return ceil ( value * pow ( 10.0, decimal ) + .5 ) / pow ( 10.0, decimal ); }```
-Prelude

touche' I forgot about floor() and ceil(). Always showin me up :p
• 03-19-2002
fastlane29
Ok, I will give it a try. thanks for the help. I will let you guys know if I figure it out.
• 03-19-2002
fastlane29
I tryied the way you guys showed me but I guess im just not savy enough to figure it out but I fooled around with the floor function and got it to work but when it is over .75 it adds the extra 1 but shows up without the decimal places for .00. (for example if I input 1.85 it will round it up to 2 but it will leave out the .00). Does this code make sense or is there a way to shorten it up some.

int main(int argc, char *argv[])
{

float number,y,remainder,hat;

cout<< "enter number";
cin>> number;

y=floor(number);
remainder=(number-y);

if(remainder>.75)
hat=y+1.00;
else
if(remainder>.50)
hat=y+.75;
else
if(remainder>.25)
hat=y+.50;
else hat=y+.25;

cout<< hat;
return 0;
}