Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Truncating a number

807600May 27 2007 — edited May 27 2007
How do I round a number? I am writing a program that converts an inputted temperature in Fahrenheit to celsius. The limitations were that the input has to be an integer, but the converted temperature has to have 1 decimal place.

So,here is a snippet of the code:

double degreesC;

//gets the user input in integer fahrenheit
degreesF = keyboard.nextInt();

//converts integer to double fahrenheit
double dblDegreesF = (double)degreesF;

//calculates celsius equivalent as a double
degreesC = (5 * (dblDegreesF - 32) / 9);

//prints the inputted integer then its equivalent celsius with one decimal
System.out.println(degreesF + " fahrenheit = " + degreesC + " celsius.");

The problem is, that the degreesC prints out with like 8 decimal places behind it. I have looked at BigDecimal, DecimalFormat, ROUND_UP, etc.. but I cannot find what is appropriate, and how to implement it into my code. I am very new to Java and I find that Java API lacks examples very much. Thanks for all the help!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 24 2007
Added on May 27 2007
8 comments
150 views