rounding to nearest penny

This is a discussion on rounding to nearest penny within the C++ Programming forums, part of the General Programming Boards category; I am a newb. this code works but i need to round to the nearest penny and can't find the ...

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

    rounding to nearest penny

    I am a newb. this code works but i need to round to the nearest penny and can't find the right function. I found floor() and ciel() but they only round up or down to the nearest integer. Any hints or ideas.
    Attached Files Attached Files

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I haven't looked at your code but one way to do what you want is to multiply by 100 to a whole integer then apply floor() or ciel() and then divide by 100 to return to the decimal.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest
    i tried floor() and ciel() but they only round up or down. i want to round to nearest penny(>= 5 round up, < 5 round down). Tried to do:

    //total is a floating value

    total = total * 100;
    total = floor(total);
    total = total/100;

    it work but is not acurate to the penny, it always rounds down.

    im not asking for the answer exactly but point me in the right direction, please.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well now all you have to do is use floor when you have to round down and ceil when you have to round up. You can check the third decimal place and if its 5 or greater round up or otherwise round down. One possible way of doing this is to use sprintf to format your number as a string in memory.search for the decimal point and then check the third char after that to decide whether to round up or down. There are probably easier ways of doing it too.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    last = number % 10; should tell you what the last number is. I think, anyway.
    I compile code with:
    Visual Studio.NET beta2

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I was soooo mashed last night...

    here's how to do it properly

    multiply by 100
    add 0.5
    floor()
    divide by 100.

    that will round to the nearest penny....

    sorry about the stoned ramblings.....
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    6
    that does the trick. i was missing the add .5.

    thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange decimal rounding
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 06-12-2008, 01:05 AM
  2. Replies: 4
    Last Post: 10-16-2006, 12:04 PM
  3. Rounding a float to the nearest half
    By kzar in forum C Programming
    Replies: 4
    Last Post: 04-01-2005, 09:07 PM
  4. Calculate nearest number?
    By electrolove in forum C Programming
    Replies: 10
    Last Post: 02-21-2003, 05:13 AM
  5. rounding
    By Kings in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2003, 02:14 PM

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