How can I round a float value (such as 37.777779) to two decimal places (37.78) in C?
Also, if you're using C++, you can just create a function like this:
string prd(const double x, const int decDigits) {
stringstream ss;
ss << fixed;
ss.precision(decDigits); // set # places after decimal
ss << x;
return ss.str();
}
You can then output any double myDouble
with n
places after the decimal point with code such as this:
std::cout << prd(myDouble,n);