Thread: How would i round up to or round off to the nearest cent?

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    33

    How would i round up to or round off to the nearest cent?

    Hello i am trying to figure out how I would easily round a double or a float to the nearest cent. It was easy enough to do it in visual basic or java, But i cannot figure out how to do it in C. I fsomebody could tell me how to do this or at least point me in the right direction it would be a great help.

    Thanks,

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you want to actually round the number in memory (hint: this is physically impossible), use an int to store integral number of pennies, or just display it with two decimal places?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    How about using "round()"?

    round, roundf, roundl functions [C99]

    Purpose:
    Rounds a floating-point value to the nearest integer.

    Syntax:
    float roundf(float x);

    double round(double x);

    long double roundl(long double x);

    Declared in:
    <math.h>

    Description:
    The function rounds x to the nearest integer value in floating-point format, rounding halfway cases away from zero, regardless of the current rounding direction.

    Returns:
    The rounded integer value.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    33
    If only if only, I can't use round(unfortunately) because i am using C90 not C99 and C90 has no round function

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Normally one uses floor or ceil for this. I prefer floor. Say if you have money = 1.455 then you can do this:

    Code:
    double money_rounded = floor(money * 100.0 + .5) / 100.0;

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I didn't realize there was a round function. Personally it always bothered me that rounding .5 to an integer is basically arbitrary. There's no reason to round either up or down.

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    33
    Thanks I hope this works, to bad i cant test it until I work out the other errors in my code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this how to round?
    By george7378 in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2011, 03:00 PM
  2. (Community Game) Round Robin for C++ [Round 01]
    By phantomotap in forum General Discussions
    Replies: 5
    Last Post: 04-18-2011, 01:07 PM
  3. (Community Game) Planning for Round Robin for C++ [Round 02]
    By phantomotap in forum General Discussions
    Replies: 2
    Last Post: 04-17-2011, 02:39 PM
  4. round
    By yes in forum C++ Programming
    Replies: 9
    Last Post: 10-11-2006, 02:11 AM
  5. Round off to nearest .50
    By arnis in forum C++ Programming
    Replies: 2
    Last Post: 11-20-2003, 09:03 AM