How to convert Celsius to Fahrenheit in Java
convert
celsius
fahrenheit
java
This article will present how to convert temperature from Celsius to Fahrenheit 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 Celsius to Fahrenheit is:
T(°F) = (T(°C) x 9/5 + 32
or
T(°F) = (T(°C) x 1.8 + 32
Using this formula, here is a conversion table with common values:
Celsius (°C) | Fahrenheit (°F) |
---|---|
-40°C | -40°F |
-30°C | -22°F |
-20°C | -4°F |
-10°C | 14°F |
0°C | 32°F |
10°C | 50°F |
20°C | 68°F |
30°C | 86°F |
40°C | 104°F |
50°C | 122°F |
60°C | 140°F |
70°C | 158°F |
80°C | 176°F |
90°C | 194°F |
100°C | 212°F |
Code
package com.admfactory;
public class ToFahrenheit {
public static double toFahrenheit(double t1) {
return t1 * 5/9 + 32;
}
public static void main(String[] args) {
System.out.println("Celsius to Fahrenheit example");
double t1 = 10d;
double t2 = toFahrenheit(t1);
System.out.println(t1 + "°C converts to " + String.format("%2f", t2) + "°F");
System.out.println();
double t3 = 140d;
double t4 = toFahrenheit(t3);
System.out.println(t3 + "°C converts to " + String.format("%2f", t4) + "°F");
}
}
Output
Celsius to Fahrenheit example
10.0°C converts to 37.555556°F
140.0°C converts to 109.777778°F