Convert Fahrenheit to Celsius with JavaScript

By  on  

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!

Recent Features

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

Incredible Demos

  • By
    Introducing MooTools NextPrev

    One thing I love doing is duplicating OS functionalities. One of the things your OS allows you to do easily is move from one item to another. Most of the time you're simply trying to get to the next or the previous item.

  • By
    MooTools Clipboard Plugin

    The ability to place content into a user's clipboard can be extremely convenient for the user. Instead of clicking and dragging down what could be a lengthy document, the user can copy the contents of a specific area by a single click of a mouse.

Discussion

  1. The reason the US (and a few other countries) don’t convert to Celsius and other metric systems has nothing to do with arrogance. Do you think that Russia is not arrogant? China? North Korea?

    The reason has to do with cost-benefit analysis. How many mental, economic, medical, social, and legal changes would have to be made, as well as the corresponding costs to time and money, to allow a complete change over. Many efforts have been made to switch the US systems officially to something else and they usually fail because the general public and industry collectively decide, often subconsciously, that it isn’t worth the change.

    To that note, the US is not unique. There are lots of countries that are officially on metric standards, yet still use other units in various places and contexts.

    Additionally, consider time. Time is represented in base 10 units at the low levels, but then it’s base 60 for seconds, base 60 for minutes, base 24 or 12 * 2 for hours, days are sometimes base 30 (but not always), and years are base 12-ish (their size changes from year to year). There have been attempts to use decimal time all around the world, but to date, no country officially uses it. Is the world just arrogant?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!