Round off to nearest .50

This is a discussion on Round off to nearest .50 within the C++ Programming forums, part of the General Programming Boards category; In my book it said that i can use this cout.setf(ios::fixed, ios::floatfield); cout.precision(2); But what if i want to round ...

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

    Round off to nearest .50

    In my book it said that i can use this

    cout.setf(ios::fixed, ios::floatfield);
    cout.precision(2);

    But what if i want to round it off to the nearest 0.50?

    like double Cash = 10.34 will be 10.50

    Thanks.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you could try configuring ceil() to work for you... you might also need floor()... I think there's a better way, but I can't think of it right now...
    Last edited by major_small; 11-20-2003 at 07:28 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    I/O functions don't do math. Just round it manually.

    Code:
    double round(double number)
    {
     double change = number - (int)number;
     number -= change;
     if(change < 0.25) change = 0;
     else if(change > 0.75) change = 1;
     else change = 0.50;
     return number + change;
    }



    ITSA
    Socket Library!

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, 03:42 PM
  2. Fibonacci Formula, How to round in C++?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 10-15-2004, 10:47 AM
  3. I need some quick help with code.
    By stehigs321 in forum C Programming
    Replies: 35
    Last Post: 10-30-2003, 09:07 PM
  4. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 08:42 PM
  5. Please help with some coding..
    By stehigs321 in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 05:44 PM

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