How do I round decimals

This is a discussion on How do I round decimals within the C++ Programming forums, part of the General Programming Boards category; I have a program to write for my cs115 class that I have to find a hat size in a ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    17

    Question 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

  2. #2
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    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?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    double round ( double value, unsigned decimal )
    {
      return ceil ( value * pow ( 10.0, decimal ) + .5 ) / pow ( 10.0, decimal );
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    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
    Last edited by biosx; 03-19-2002 at 06:56 PM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    17
    Ok, I will give it a try. thanks for the help. I will let you guys know if I figure it out.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    17

    How about this

    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  2. Fibonacci Formula, How to round in C++?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 10-15-2004, 11:47 AM
  3. I need some quick help with code.
    By stehigs321 in forum C Programming
    Replies: 35
    Last Post: 10-30-2003, 10:07 PM
  4. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 09:42 PM
  5. Please help with some coding..
    By stehigs321 in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21