Truncating a number
807600May 27 2007 — edited May 27 2007How 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!