Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Rounding to the _nths place [duplicate]

Tags:

c++

I'm currently learning c++ on a linux machine. I've got the following code for rounding down or up accordingly but only whole numbers. How would I change this code to round to the hundredths place or any other decimal? I wouldn't ask if I didn't try looking everywhere already :( and some answers seem to have a ton of lines for what seems to be a simple function!

double round( double ){
return floor(value + 0.5 );
}
like image 602
Tek Avatar asked Dec 28 '22 10:12

Tek


1 Answers

Try

double round( double value )
{
    return floor( value*100 + 0.5 )/100;
}

to round to two decimal places.

like image 83
Martin B Avatar answered Jan 13 '23 05:01

Martin B