The way I round numbers is by adding 0.5 and casting it to an integer. That rounds to the nearest whole number. To round to a different decimal precision, first multiply the number by 10^n, round that to the nearest whole number, then divide by 10^n.
void main(){
float num = 0;
while (true){//CTRL + C to quit
cin>>num;
cout<<"num rounded down to nearest integer "<<floor(num)<<endl;
cout<<"num rounded up to nearest integer "<<ceil(num)<<endl;
cout<<"num rounded nearest to nearest integer "<<floor(num + 0.5)<<endl;
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.