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

Printable View

- 10-31-2006DaesomRounding Question
Is there a command to cause a solution to round a number to a place? example 5.67 to 5.7?

- 10-31-2006bumfluff
you could just store each digit in a vector of ints and then just round it manually.

- 10-31-2006Daesom
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.

- 10-31-2006DougDbug
**precision()**should round for you. Take a look at the example at__cppreference.com__ - 10-31-2006Daesom
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

- 10-31-2006Daved
.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.