Thread: Rounding to Hundreths

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    5

    Question Rounding to Hundreths

    I have a little program that calculates the bill of burgers, cokes, etc. but I want to round the tax so that it doesn't go 4 places (only 2 like $).

    Now, that may be simple, but I would prefer that if there are any digits after the hundreths, it automatically goes up:

    $0.4109 --> $0.42

    Hope you can help.

    Thanks

  2. #2
    Unregistered
    Guest
    Use the ceil function in math.h

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main ( ) {
        double p = 3.4109;
        p = ceil( p * 100 ) / 100;
        printf( "%.2f\n", p );
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    5
    I don't have the math.h library. Should I?

  4. #4
    Unregistered
    Guest
    This being C++, maybe its called
    #include <cmath>

    If you have neither, you have a seriously broken compiler - it's supposed to be a standard feature.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    5
    OK, I found it. It's there, I just mistyped the #include before. But now that I found the math.h library, I need to initialize printf. Should I just do so and make it 0 at the top?

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    5

    Error: implicit declaration of function 'int printf(...)'

    That subject line was the new error before I tried to initialize printf. I realize that it's a function, but it ain't workin

  7. #7
    Unregistered
    Guest
    or use cout, since this is C++

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    5
    Beautiful, man! How did you figure that out? I looked at that ceil function, but I still can't see how it's different than the others in the math.h library.

    Another side question: Can you use images for outputs like in BASIC? Basically, I want to line up the decimals so it doesn't look funny if the bill is $100, while the tax is $7.

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rounding numbers
    By maple23 in forum C Programming
    Replies: 3
    Last Post: 05-26-2008, 12:33 AM
  2. Rounding Float- Return not happening ?
    By pprabhakar in forum C Programming
    Replies: 5
    Last Post: 05-14-2006, 09:44 AM
  3. setprecision() - can I count on it rounding or not?
    By major_small in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2005, 02:26 PM
  4. Help with rounding a number
    By nickk in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 11:44 AM
  5. Rounding & DOUBLE type
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-17-2002, 05:29 PM