# round() function

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 04-09-2003
Stoned_Coder
if this can round to an int....

int x = floor( your_float_or_double + 0.5 );

then it can be made to work for a float something like....

float x = floor( 100*(your_float_or_double + 0.5) ) / 100.0;

notice how we move the decimal point two places by multiplying with 100 so that floor can find an int then dividing result by 100 gives decimal to 2 decimal places. Thats what u want isnt it.See it wasnt hard. You were almost given the answer by your teacher and i tried not to give it to u but instead make you think for yourself but as you couldn't work it out I have shown you the guts.
Firstly wrap in a function.
Secondly a bit harder, can you make a function that will round to a passed number of decimal points.Its not too hard.
• 04-09-2003
vege^
yeah i kinda realized it just now :/

this is what i did

Code:

`f=floor(d*100)/100.0;`
soz, but thx :)
• 04-09-2003
Stoned_Coder
that wont round it will floor.
To round either floor(d+0.5) or ceil(d-0.5).

That extra 0.5 is important. This is because flooring 4.6 will give 4 but flooring 4.6+0.5 will give 5 a correct rounding off.
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12