Oct 232017

ADM

How to convert Fahrenheit to Celsius in Java

  • 23 October 2017
  • ADM

 

 

 

How to convert Fahrenheit to Celsius in Java - images/logos/java.jpg

 

This article will present how to convert temperature from Fahrenheit to Celsius scale and a simple implementation in Java.

The Fahrenheit temperature scale is named for German physicist Daniel Gabriel Fahrenheit and is used primarily in the United States.

The Celsius temperature scale-originally centigrade and later renamed for Swedish astronomer Anders Celsius-is used almost everywhere else in the world.

The formula to convert Fahrenheit to Celsius is:

T(°C) = (T(°F) - 32) x 5/9

playing a little with the formula you can have

T(°C) = (T(°F) - 32) / (9/5)

or

T(°C) = (T(°F) - 32) / 1.8

Using this formula, here is a conversion table with common values:

Fahrenheit (°F) Celsius (°C)
-459.67 °F -273.15 °C
-50 °F -45.56 °C
-40 °F -40.00 °C
-30 °F -34.44 °C
-20 °F -28.89 °C
-10 °F -23.33 °C
0 °F -17.78 °C
10 °F -12.22 °C
20 °F -6.67 °C
30 °F -1.11 °C
40 °F 4.44 °C
50 °F 10.00 °C
60 °F 15.56 °C
70 °F 21.11 °C
80 °F 26.67 °C
90 °F 32.22 °C
100 °F 37.78 °C
110 °F 43.33 °C
120 °F 48.89 °C
130 °F 54.44 °C
140 °F 60.00 °C
150 °F 65.56 °C
160 °F 71.11 °C
170 °F 76.67 °C
180 °F 82.22 °C
190 °F 87.78 °C
200 °F 93.33 °C
300 °F 148.89 °C
400 °F 204.44 °C
500 °F 260.00 °C
600 °F 315.56 °C
700 °F 371.11 °C
800 °F 426.67 °C
900 °F 482.22 °C
1000 °F 537.78 °C

Code

package com.admfactory;

public class ToCelsius {

    public static double toCelsius(double t1) {
	return (t1 - 32) * 5/9;
    }

    public static void main(String[] args) {
	System.out.println("Fahrenheit to Celsius example");
	double t1 = 10d;
	double t2 = toCelsius(t1);
	System.out.println(t1 + "°F converts to " + String.format("%2f", t2) + "°C");
	System.out.println();
	double t3 = 140d;
	double t4 = toCelsius(t3);
	System.out.println(t3 + "°F converts to " + String.format("%2f", t4) + "°C");
    }
}

Output

Fahrenheit to Celsius example
10.0°F converts to -12.222222°C

140.0°F converts to 60.000000°C