Thread: Round off to nearest .50

  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 08: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,708
    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;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 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, 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, 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