Is there a way to round to 2 decimal places?

This is a discussion on Is there a way to round to 2 decimal places? within the C Programming forums, part of the General Programming Boards category; Hi all, I've got a program which returns various currencies to the user, so I need these to be rounded ...

  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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    > 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, 01: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

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