Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

rounding numbers to 10ths, 100ths or 1000ths

Writer Matthew Martinez
$\begingroup$

I was going through the C++ 10th edition deitel's book. There I encountered a questioned regarding floor() function. And here's the question:Floor() practice questionNow, from what I understand so far is that no matter what number I set as x I get the same output from both expressions.

Can anyone simplify this for me? How are the above expressions any different from one another? And how are they rounding the input in either the tenths or hundredths form?

$\endgroup$ 4

1 Answer

$\begingroup$

Take $x=0.7152$, for example. The first expression yields:$$\text{floor}(x\cdot 10+0.5)/10=\text{floor}(7.652)/10=7/10=0.7$$ while the second expression yields:$$\text{floor}(x\cdot 100+0.5)/100=\text{floor}(72.02)/100=72/100=0.72$$ Both results represent correct rounding of $x$ to one and two places after the decimal point, respectively.

$\endgroup$

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy