Thread: Is there a way to round to 2 decimal places?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    42

    Is there a way to round to 2 decimal places?

    Hi all,

    I've got a program which returns various currencies to the user, so I need these to be rounded to two decimal places. I've searched the boards and everyone just talks about ceil() and floor(), which aren't really enough.

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could multiply by 100, do the rounding, then divide back.

    Incidentally, using floating point for money may result in undesirable inaccuracy. It might be better to use integers, or some custom decimal type instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You can do
    printf("%.2f", myFloat );

    Or multiply by 100 and take the integer part of the result. Then use /100 and %100 to extract the parts you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by Salem View Post
    You can do
    printf("%.2f", myFloat );
    Bingo! Nice one

    Thanks guys.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by osiris^ View Post
    Bingo! Nice one

    Thanks guys.
    Again though, using floats for monetary values is a bad idea.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The reason you shouldn't use floating point for money-counting is that the result of a floating point calculation isn't PRECISE. Pariticularly certain numbers can not be described exactly in floating point, for example 0.7. So if you ever take 70% of something, it will be 69.999999999...% of it. (or something is 70 pence/cents/öre or whatever the small part of the currency is called)

    The real problem here is that if you enter a new value, say someone pays in a check for an invoice, you enter 123.71 as the amount, and you compare 123.71 with the value you calculated (which PRINTED as 123.71, but the actual value is 123.7091214823), it will not be precisely that. So a comper like
    Code:
      if (paid == invoiced) { 
         invoice.fully_paid = TRUE;
      }
    will fail because the numbers don't match up.

    As other said, count the money in a LONG integer and use it to indicate the smaller part of the currency (pence, cents or whatever), and then just make a special print routine to print the value as (v / 100) . (v % 100). If you deal with really large amounts, you may want to use a 64 bit integer.

    Of course, in C++ you could implement a "monetary" class, which does this automagically.

    --
    Mats

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Ah, that makes sense then. Thanks

    On a side note, is it possible to have a £ sign in C? I'm presuming you have to use some unicode value, because when it was printed to the screen, it came out as a "u" with an accent on it.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Your options include but are probably not limited to: (from easier to harder)

    1. Use pounds, but suffix the amount with GBP, and curse ASCII.
    2. Convert to dollars, use dollars, and curse ASCII.
    3. Attempt to use Unicode or multibyte strings, and curse ASCII.

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    In college, using Borland, I could get pound symbols by holding down alt and typing 0162 (or something like that). Dosent seem to work at home in DevC++ tho. So I guess I just use GBP and curse ASCII.

    [Edit] Actually no need to curse ASCII:
    Code:
    #include <stdio.h>
    int main()
    {
        putchar(156);
        getchar();
    }
    [/edit]
    Last edited by mike_g; 07-27-2007 at 12:33 PM.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    > putchar(156);

    That's from the extended set, and there are a couple, so stick to the standard one. Vanilla char might not be the same as unsigned char either, which might lead to wrong output as well.

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh yeah, I guess that might cause problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 06-08-2008, 09:04 AM
  2. Round conversion to 2 decimal places
    By Cero.Uno in forum C Programming
    Replies: 4
    Last Post: 02-17-2008, 02:39 AM
  3. Decimal places
    By Gordon in forum Windows Programming
    Replies: 4
    Last Post: 09-28-2007, 10:03 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Replies: 10
    Last Post: 06-12-2002, 03:15 PM