How to Convert Celsius to Fahrenheit in Java?

Photo of author

This guide will teach you how to convert Celsius to Fahrenheit in Java and vice versa. Conversion from Celsius to Fahrenheit refers to changing a temperature value’s scale from Celsius to Fahrenheit.

There are four ways to convert between Celsius and Fahrenheit: formula, method, Bigdecimal, and DecimalFormat. A specific formula may change a temperature value from Celsius to Fahrenheit. Fahrenheit is med as (Celsius * 9/5) + 32.

Learn more about converting Celsius to Fahrenheit in Java by reading on.

See also: How To Import A Package In Java? Full Guide

Celsius to Fahrenheit Formula

The following equation is as follows to convert between degrees Celsius and degrees Fahrenheit:

°F = °C * (9/5) + 32

convert celsius to farenheit

We may convert the temperature indicated in Celsius to Fahrenheit using the formula above. The Celsius to Fahrenheit Java program to convert between will likewise be coded using this algorithm. Now, let’s convert Celsius to Fahrenheit in Java.

How to convert Celsius to Fahrenheit in Java?

Here are several alternative ways to convert Celsius to Fahrenheit in Java.

Using the formula

This solution uses the formula (Celsius * 9/5) + 32 to convert the temperature from Celsius to Fahrenheit. When the user enters a temperature in Celsius, the temperature in Fahrenheit is converted and printed in place of the Celsius reading.


Package com.javacodepoint.conversion;

import java.util.Scanner;

public class CelsiusToFahrenheit {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the temperature in Celsius: ");

double celsius = scanner.nextDouble();

double fahrenheit = (celsius * 9 / 5) + 32;

System.out.println("Temperature in Fahrenheit: " + fahrenheit);

}

}

OUTPUT:

Enter the temperature in Celsius: 36

Temperature in Fahrenheit: 96.8

Using a method

The Java temperature conversion is handled in this approach by a separate method called convertCelsiusToFahrenheit. The procedure converts a temperature measured in Celsius into its matching Fahrenheit value. The converted Java Celsius to Fahrenheit temperature is then printed in the primary approach.


package com.javacodepoint.conversion;

import java.util.Scanner;

public class CelsiusToFahrenheit2 {

public static double convertCelsiusToFahrenheit(double celsius) {

return (celsius * 9 / 5) + 32;

}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the temperature in Celsius: ");

double celsius = scanner.nextDouble();

double fahrenheit = convertCelsiusToFahrenheit(celsius);

System.out.println("Temperature in Fahrenheit: " + fahrenheit);

}

}

OUTPUT:

Enter the temperature in Celsius: 100

Temperature in Fahrenheit: 212.0

Using BigDecimal

This Java conversion from Celsius to Fahrenheit uses the calculation (Celsius * 9/5) + 32 to determine the temperature in Fahrenheit. For handling decimal accuracy, there is a class called BigDecimal. A BigDecimal object that stores the temperature in Fahrenheit is printed.


package com.javacodepoint.conversion;

import java.math.BigDecimal;

import java.util.Scanner;

public class CelsiusToFahrenheit3 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the temperature in Celsius: ");

double celsius = scanner.nextDouble();

BigDecimal fahrenheit = BigDecimal.valueOf((celsius * 9 / 5) + 32);

System.out.println("Temperature in Fahrenheit: " + fahrenheit);

}

}

OUTPUT:

Enter the temperature in Celsius: 40

Temperature in Fahrenheit: 104.0

Using DecimalFormat for formatting

The two decimal places for the Fahrenheit temperature are formatted in this approach using the DecimalFormat class. The formatted Fahrenheit temperature is printed when the DecimalFormat instance is constructed using the pattern #.## to provide two decimal places.


package com.javacodepoint.conversion;

import java.text.DecimalFormat;

import java.util.Scanner;

public class CelsiusToFahrenheit4 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the temperature in Celsius: ");

double celsius = scanner.nextDouble();

double fahrenheit = (celsius * 9 / 5) + 32;

DecimalFormat decimalFormat = new DecimalFormat("#.##");

String formattedFahrenheit = decimalFormat.format(fahrenheit);

System.out.println("Temperature in Fahrenheit: " + formattedFahrenheit);

}

}

OUTPUT:

Enter the temperature in Celsius: 32

Temperature in Fahrenheit: 89.6

You now understand how to convert Celsius to Fahrenheit in Java.

Read also: How To Make A Game In Java: Java 2D Gaming

Fahrenheit to Celsius Formula

The following equation is as follows to convert between degrees Fahrenheit and degrees Celsius:

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

fahrenheit to celsius

We may convert the temperature provided in Fahrenheit to Celsius using the formula above. This formula will also be used to construct the Java program that converts temperatures from Fahrenheit to Celsius.

Fahrenheit to Celsius Conversion Java Programs

Let’s talk about how to convert Fahrenheit to Celsius in Java after learning how to convert Celsius to Fahrenheit in Java. Here are several options for converting a temperature in Java between Fahrenheit and Celsius:

Using the formula

The temperature is changed from Fahrenheit to Celsius using this solution’s formula (Fahrenheit – 32) * 5/9. The temperature is requested in Fahrenheit, converted, and printed in Celsius once the user clicks the OK button.


package com.javacodepoint.conversion;

import java.util.Scanner;

public class FahrenheitToCelsius {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the temperature in Fahrenheit: ");

double fahrenheit = scanner.nextDouble();

double celsius = (fahrenheit - 32) * 5 / 9;

System.out.println("Temperature in Celsius: " + celsius);

}

}

OUTPUT:

Enter the temperature in Fahrenheit: 97

Temperature in Celsius: 36.111111111111114

Using a method

This approach defines a separate method named convert FahrenheitToCelsius to carry out the conversion. The procedure receives the temperature input in Fahrenheit and outputs the equivalent temperature in Celsius.


package com.javacodepoint.conversion;

import java.util.Scanner;

public class FahrenheitToCelsius2 {

public static double convertFahrenheitToCelsius(double Fahrenheit) {

return (Fahrenheit - 32) * 5 / 9;

}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the temperature in Fahrenheit: ");

double fahrenheit = scanner.nextDouble();

double celsius = convertFahrenheitToCelsius(fahrenheit);

System.out.println("Temperature in Celsius: " + celsius);

}

}

OUTPUT:

Enter the temperature in Fahrenheit: 100

Temperature in Celsius: 37.77777777777778

Using DecimalFormat for formatting

Using the DecimalFormat class, this method formats the two decimal places for the Celsius temperature. With the pattern #.##, two decimal places are specified when creating the DecimalFormat instance, and the formatted Celsius temperature is displayed.


package com.javacodepoint.conversion;

import java.text.DecimalFormat;

import java.util.Scanner;

public class FahrenheitToCelsius3 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the temperature in Fahrenheit: ");

double fahrenheit = scanner.nextDouble();

double celsius = (fahrenheit - 32) * 5 / 9;

DecimalFormat decimalFormat = new DecimalFormat("#.##");

String formattedCelsius = decimalFormat.format(celsius);

System.out.println("Temperature in Celsius: " + formattedCelsius);

}

}

OUTPUT:

Enter the temperature in Fahrenheit: 100

Temperature in Celsius: 37.78

FAQs

How does JavaScript read degrees Fahrenheit?

The user uses the prompt () function to read the degree Fahrenheit value. The parseFloat () function turns the value read from the user into a floating-point number. One may get the Celsius value by deducting 32 from the Fahrenheit number and dividing the result by 1.8.

What temperature does a Celsius thermometer measure?

A standard Celsius thermometer's range is 0°C (the temperature at which water becomes ice under normal air pressure) to 100°C (the temperature at which water will boil under normal air pressure).

What does 28.5 Celsius convert to in Fahrenheit?

83.3 degrees Fahrenheit is equivalent to 28.5 degrees Celsius.

How can we convert a temperature in JFrame to Celsius?

There are three buttons and two text fields in a JFrame. The input temperature will be in the first text box. The temperature conversion will be in the second text area. The first button, F2C, will change the inputted value from the first text field's numeric input to Celsius and enter it in the second text field.

Conclusion

We hope this post about converting Celsius to Fahrenheit in Java is helpful. We have also discussed the conversion of Fahrenheit to Celsius in Java. The fundamental idea behind the Celsius temperature scale is that water freezes at 0 °C and boils at 100 °C. Water freezes at 32 °F and it boils at 212 °F according to the Fahrenheit scale, which measures temperature.

Check this out: Java Class Loaders: Deep Dive Into Java’s Core Mechanism

Leave a Comment