I am trying to find a round function in the standard library but I didn’t see one. Is there any way to round a double to n decimals in c++?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
C++11 has std::round in
<cmath>.Without that you can use std::floor and std::ceil on adjusted numbers. E.g.
std::floor(n * 100 + 0.5)/100to round to two decimal places.Although it should be noted that rounding isn’t entirely trivial; There are complications such as choosing to round toward zero, toward negative infinity, round to even, etc. If you’re writing programs for production be sure you understand the rounding requirements for your domain.