Select Page

Convert Fahrenheit to Celsius with JavaScript

David Walsh
Published: October 26, 2022

The United States is one of the last bodies that refuses to implement the Celsius temperature standard. Why? Because we’re arrogant and feel like we don’t need to change. With that said, if you code for users outside the US, it’s important to provide localized weather data to users. Let’s took at how you can convert between Fahrenheit and Celsius.

Fahrenheit to Celsius

The formula to convert Fahrenheit to Celsius is:

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

The following function converts Fahrenheit to Celsius:

```function convertFahrenheitToCelsius(degrees) {
return Math.floor(5 / 9 * (degrees - 32));
}
```

Celsius to Fahrenheit

The formula to convert Celsius to Fahrenheit is:

```°F = (°C × 9/5) + 32
```

The following function converts Celsius to Fahrenheit:

```function convertCelsiusToFahrenheit(degrees) {
return Math.floor(degrees * (9/5) + 32);
}
```

Temperature conversion is one of those things that’s difficult to do in your head due to the somewhat complex formula. If you have a site that reflects weather data, keep these handy functions nearby!

• JavaScript Promise API

While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why “hold up the show” when you can trigger numerous requests at once and then handle them when each is ready?  Promises are becoming a big part of the JavaScript world…

• MooTools Fun with Fx.Shake

Adding movement to your website is a great way to attract attention to specific elements that you want users to notice. Of course you could use Flash or an animated GIF to achieve the movement effect but graphics can be difficult to maintain. Enter…

• MooTools Wall Plugin

One of the more impressive MooTools plugins to hit the Forge recently was The Wall by Marco Dell’Anna.  The Wall creates an endless grid of elements which can be grabbed and dragged, fading in elements as they are encountered.  Let me show…

Source: davidwalsh.name