Algobook
- The developer's handbook
mode-switch
back-button
Buy Me A Coffee
Thu Apr 20 2023

How to convert Celsius to Farenheit in JavaScript

In this tutorial we will do some conversion. We will first convert Celsius to Farenheit, and then Farenheit to Celsius. I got the idea to do this guide when I developed our simple weather widget where I did some conversion work.

Formulas

We start with the formula of converting Celsius to Farenheit. It goes like this.

F = (C × 1.8) + 32

And Farenheit to Celsius is like this:

C = (F - 32) / 1.8;

Not to complicated, right? Let's do some code!

Celsius to Farenheit

const celsiusToFarenheit = (c) => Math.round(c * 1.8 + 32);

We will use Math.round() to get a whole number instead of getting decimals. Now, let's test it.

console.log(celsiusToFarenheit(29)); // 84 console.log(celsiusToFarenheit(32)); // 90 console.log(celsiusToFarenheit(5)); // 41

Farenheit to Celsius

All right, let't do the opposite.

const farenheitToCelsius = (f) => Math.round((f - 32) / 1.8);

And let's test it with the results of our previous test to see if it gets back to its original value...

console.log(farenheitToCelsius(84)); // 29 console.log(farenheitToCelsius(90)); // 32 console.log(farenheitToCelsius(41)); // 5

We did it!!

Outro

In this short guide, we demonstrated how to convert temperatures from Celsius to Farenheit and then back again to celsius. I hope you got some value out of this guide, and good luck in your current project!

Have a great day!

signatureThu Apr 20 2023
See all our articles