Math.Floor may be used to round a number to a specific decimal place. The statem
ID: 3631520 • Letter: M
Question
Math.Floor may be used to round a number to a specific decimal place. The statement
y = Math.Floor( x * 10 + 0.5 ) / 10;
rounds x to the tenths position (i.e., the first position to the right of the decimal point). The statement
y = Math.Floor( x * 100 + 0.5 ) / 100;
rounds x to the hundredths position (i.e., the second position to the right of the decimal point). Write an application that defines four methods for rounding a number x in various ways:
a.
RoundToInteger( number )
b.
RoundToTenths( number )
c.
RoundToHundredths( number )
d.
RoundToThousandths( number )
For each value read, your application should display the original value, the number rounded to the nearest integer, the number rounded to the nearest tenth, the number rounded to the nearest hundredth and the number rounded to the nearest thousandth.
Explanation / Answer
RoundToInteger( number )
{ printf("%f",number);
printf("%f",Math.floor(number + 0.5 ));
}
RoundToTenths( number )
{
printf("%f",Math.Floor( number * 10 + 0.5 ) / 10);
}
RoundToHundredths( number )
{
printf("%f",Math.Floor( number * 100 + 0.5 ) / 100);
}
RoundToThousandths( number )
{
printf("%f",Math.Floor( number * 1000 + 0.5 ) / 1000);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.