Is there a command to cause a solution to round a number to a place? example 5.67 to 5.7?
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?...
Is there a command to cause a solution to round a number to a place? example 5.67 to 5.7?
you could just store each digit in a vector of ints and then just round it manually.
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.
precision() should round for you. Take a look at the example at cppreference.com
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
.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.