Jul 7, 2012 at 9:23am UTC
I´m working on a problem and part of the problem is to find the average of an array and i find it but it comes out with a lot of decimals. So how can I circle it to two decimals ?
Jul 7, 2012 at 9:41am UTC
You can use the setprecision function that's found in <iomanip>
http://cplusplus.com/reference/iostream/manipulators/setprecision/
1 2 3 4 5 6 7 8 9
#include <iostream>
#include <iomanip>
int main()
{
double pi = 3.141592653589793238462643383279502884197169399375105820974944;
std::cout << std::fixed << std::setprecision(2) << pi;
return 0;
}
3.14
Last edited on Jul 7, 2012 at 9:46am UTC
Jul 7, 2012 at 12:43pm UTC
do you know those 60 decimals of pi by heart or did you just copy it ? ;d
Jul 7, 2012 at 1:25pm UTC
He has calculated the pi just now.
Jul 7, 2012 at 3:27pm UTC
How do you get the output next to the code?