Rounding decimal with conversion of double

Conversion from Farenheit to Celcius

Page 1 of 1

3 Replies - 1443 Views - Last Post: 30 January 2010 - 09:43 AM Rate Topic: -----

#1 osiris131313   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-January 10

Rounding decimal with conversion of double

Post icon  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.

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);

   }

}




Is This A Good Question/Topic? 0
  • +

Replies To: Rounding decimal with conversion of double

#2 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

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:
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
Was This Post Helpful? 0
  • +
  • -

#3 osiris131313   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-January 10

Re: Rounding decimal with conversion of double

Posted 30 January 2010 - 04:46 AM

Thank you very much. The website helped me out tremendously!
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1