To round numbers to specific decimal places, use a statement like double y = Math.floor(x * 10

Question:

To round numbers to specific decimal places, use a statement like double y = Math.floor(x * 10 + 0.5) / 10;

which rounds x to the tenths position (i.e., the first position to the right of the decimal point), or double y = Math.floor(x * 100 + 0.5) / 100;

which 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 program 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.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question

Java How To Program Late Objects Version

ISBN: 9780136123712

8th Edition

Authors: Paul Deitel, Deitel & Associates

Question Posted: