import java.util.Scanner;
public class Conversion
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
double x;
double result;
System.out.println("Enter temperature in Fahrenheit");
x = input.nextInt();
result = (5.0/9)*(x-32);
System.out.println("The temperature in Celsius =" + result);
}
}
Rounding decimal with conversion of doubleConversion from Farenheit to Celcius
Page 1 of 1
3 Replies - 1443 Views - Last Post: 30 January 2010 - 09:43 AM
#1
Rounding decimal with conversion of double
Posted 30 January 2010 - 04:06 AM
I am just learning how to use java and my first assignment was to convert Farenheit to Celcius in the precision of one decimal place. I looked on some websites and I was getting int*10/10, but when i did so it did not work. I also tried to understand DecimalFormat, but epicly failed. Any help would be greatly appreciated. Here is my attached code underneath with just the compiled Farenheit to Celcius conversion without an attempt at inserting a rounding function.
Replies To: Rounding decimal with conversion of double
#2
Re: Rounding decimal with conversion of double
Posted 30 January 2010 - 04:23 AM
x is double, then in order to assign it a double value you might want to use:
also, here is a good page on DecimalFormat, you can see what format fits you most:
http://java.sun.com/...imalFormat.html
x = input.nextDouble();
also, here is a good page on DecimalFormat, you can see what format fits you most:
http://java.sun.com/...imalFormat.html
#3
Re: Rounding decimal with conversion of double
Posted 30 January 2010 - 04:46 AM
Thank you very much. The website helped me out tremendously!
#4
Re: Rounding decimal with conversion of double
Posted 30 January 2010 - 09:43 AM
You can also use the System.out.printf() method with the %f formatter. It works something like this: System.out.printf("%.2f", doubleVariableName); , which rounds floating-point numbers (double and float) to two decimal places of precision.
Page 1 of 1

New Topic/Question
Reply



MultiQuote





|