Hi, I`m making a temperature program, the program takes the temperature F and prints the equivalent Celcius temperature. and VS.
The temperature is a double, but I get too many numbers after the decimal poit, like( 45.67777777...) I`d like to set it to only one or two numbers after the decimal.
How do I do that, I`ve been looking in a book, but I don`t know what the method is called.

Thanks

I think I found what I`m looking for, but it`s not working how I want it to, it`s no doing anything the way I have it set up.
I put my formating function inside the convertTemp(), that`s probably not where it should be.

This is my code:

import java.text.DecimalFormat;
  
    public class Temperature
   {
      private double temp;
      private char tempScale;
   
       public Temperature()
      {
      
      }
   
       public Temperature(double t, char tempS)
      {
         setTemp (t);
         setTempScale (tempS);
      }
   
   
       void displayTemp()
      {		
      	
         if (getTempScale() == 'F')
         {
            System.out.println("The current temperature is " + getTemp() + " " + getTempScale());
         }
         else
         {
            System.out.println("Which is equivalent to " + getTemp() + " " + getTempScale());
         }
      }
   	
      DecimalFormat oneDigit = new DecimalFormat("0.0");
   
       double convertTemp()
      {
         if (tempScale == 'F')
         {
            temp = (temp - 32)* 5/9;
            tempScale = 'C';
         }
         else
         {
            temp = temp * 9 / 5 + 32;
            tempScale = 'F';
         }
      	
         oneDigit.format(temp);
      
         return 0;
      }
   
       int tempState()
      {
      
         if(getTemp() >= 212 && getTempScale() == 'F' || getTemp() >= 100 && getTempScale() == 'C')
         {
            System.out.print("Water at this temperature is a gas.\n");
         }
         else if(getTemp() >= 32 && getTempScale() == 'F' || getTemp() >= 0 && getTempScale() == 'C')
         {
            System.out.print("Water at this temperature is a liquid.\n");
         }
         else
         {
            System.out.print("Water at this temperature is solid.\n");
         }
      
         return 0;
      }
   
      
   
       void setTemp(double value)
      {
         temp = value;
      }
   
       void setTempScale(char type)
      {
         tempScale = type;
      }
   
       double getTemp()
      {
        
         return temp;
      }
   
       char getTempScale()
      {
      	
         return tempScale;
      }
   }

and my testCode:

class TestTemp
{
	public static void main(String[] args)
	{
		Temperature theTemp = new Temperature(275, 'F');
		
		while(theTemp.getTemp() >= (-10))
		{
			theTemp.displayTemp();
			theTemp.convertTemp();
			theTemp.displayTemp();
			theTemp.convertTemp();
			theTemp.tempState();
			System.out.println();
			theTemp.setTemp(theTemp.getTemp() - 25);
				
		}		
	}
}
This article has been dead for over six months. Start a new discussion instead.