Thread: How To Round A Float To Two Decimal Points?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1

    Question How To Round A Float To Two Decimal Points?

    Could some tell me how to round a float number to two decimal points?

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    .
    Last edited by Troll_King; 12-01-2001 at 05:58 AM.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    I think setprecision truncates variable at given length, but I don't think it rounds it up or down at that length.

    If it truncates rather than rounds, you can create your own rounding function by taking current variable, mulitply it by 100 to move decimal place two places to the right, cast it to int to get rid of remaining decimal placeholders, and finding the modulo of 10 of the resuling int. If the modulo is greater than 4 add 1 to the int, otherwise don't. Divide the int by 100 to get the value rounded to two decimals.

    I'm sure there is an easier way, but that should work.
    Last edited by guest; 11-09-2001 at 11:49 AM.

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. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM
  4. Need more eyes to find problem??
    By sailci in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2002, 10:03 PM
  5. setting a float to 4 digit decimal precision
    By river-wind in forum C Programming
    Replies: 8
    Last Post: 01-21-2002, 05:03 PM