rounding numbers to 10ths, 100ths or 1000ths
Matthew Martinez
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$ 41 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$