Java Data Type How to - Convert Fahrenheit to Celsius back and forth with float








Question

We would like to know how to convert Fahrenheit to Celsius back and forth with float.

Answer

public class Main {
  public static void main(String[] arguments) {
    float fah = 86;
    System.out.println(fah + " degrees Fahrenheit is ...");
    fah = fah - 32;//from ww w. j  a va 2s.co m
    fah = fah / 9;
    fah = fah * 5;
    System.out.println(fah + " degrees Celsius\n");

    float cel = 33;
    System.out.println(cel + " degrees Celsius is ...");
    cel = cel * 9;
    cel = cel / 5;
    cel = cel + 32;
    System.out.println(cel + " degrees Fahrenheit");
  }
}

The code above generates the following result.