Rounding Question

This is a discussion on Rounding Question within the C++ Programming forums, part of the General Programming Boards category; Is there a command to cause a solution to round a number to a place? example 5.67 to 5.7?...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23

    Rounding Question

    Is there a command to cause a solution to round a number to a place? example 5.67 to 5.7?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    you could just store each digit in a vector of ints and then just round it manually.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    well for what I am doing I dout that will work, heh... I have posted my code before, just started working on it again, trying to figure it out.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    precision() should round for you. Take a look at the example at cppreference.com

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    Yikes, I was worried about that. I just set my precision much higher to check the full answer and it is showing .15, i want the number to show .0, I guess this is a big problem

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    .15 doesn't round to .0. It rounds to .2 or 0 depending on whether you want to round to the ones or tenths place. To round a decimal number, multiply it by 10 to the power of the place you want to round, for example, multiply 0.167 by 100 to get 16.7. Then add 0.5, and pass it to floor(). So it becomes 17.2 and the result of floor(17.2) is 17.0. Finally, divide back by the original power of 10, so 17.0 divided by 100 is 0.17. If the number is negative you will want to subtract the 0.5 instead of add it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  3. rounding question
    By keithmolo in forum C++ Programming
    Replies: 12
    Last Post: 08-14-2003, 09:33 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

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