Dot Net Perls

Convert Degrees Celsius to Fahrenheit - C#

by Sam Allen

Problem

You need to convert degrees Celsius to degrees Fahrenheit using C# methods. It is more convenient to adapt proven code than write the same code again. Compare the results to Google to make sure they are accurate.

Input - beforeOutput - after
100 °C212 °F
212 °F100 °C
50 °C122 °F
122 °F50 °C

Solution: numeric conversion in C#

You can convert Celsius to Fahrenheit and back again using the constant 5/9. The .NET runtime will JIT the division, turning it into a constant, so it will perform well. The two important methods next are contained in a static class.

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("{0} = {1}",
            100,
            ConvertTemp.ConvertCelsiusToFahrenheit(100));

        Console.WriteLine("{0} = {1}",
            212,
            ConvertTemp.ConvertFahrenheitToCelsius(212));

        Console.WriteLine("{0} = {1}",
            50,
            ConvertTemp.ConvertCelsiusToFahrenheit(50));

        Console.WriteLine("{0} = {1}",
            122,
            ConvertTemp.ConvertFahrenheitToCelsius(122));
    }

    static class ConvertTemp
    {
        public static double ConvertCelsiusToFahrenheit(double c)
        {
            return ((9.0 / 5.0) * c) + 32;
        }

        public static double ConvertFahrenheitToCelsius(double f)
        {
            return (5.0 / 9.0) * (f - 32);
        }
    }
}

Example: some output from the program

When you compile and run the code sample at the top of this page, you will get this output. These numbers are identical to those Google returns with the conversion queries.

100 = 212
212 = 100
50 = 122
122 = 50

Example: converting the temperature of the human body

If you are reading this, you are a human, so your body temperature should be close to the standard, which is around 98.6 °F. Note that this actually varies between people, so don't be alarmed if it is slightly different.

using System;

class Program
{
    static void Main()
    {
        //
        // Show the temperature of the human body in Celsius.
        //
        double h = 98.6;
        double hc = ConvertTemp.ConvertFahrenheitToCelsius(h);
        Console.WriteLine("{0}, {1}",
            h.ToString(),
            hc.ToString("00.0"));
        // 98.6, 37.0
    }
}

Summary: converting temperatures in C#

Here we saw two methods in a static class and tested them against Google's conversion function. Scientific applications may need more elaborate methods, but these suffice normally.

The medical advice here, in the section about the human body temperature, does not substitute for your local doctor's advice. If you have a medical concern, don't come to Dot Net Perls, but instead ask a medical professional.

Dot Net Perls
About
Sitemap
Source code
RSS
Conversion
Convert List to Array
Convert Char Array to String
Convert Bytes to Megabytes
C# Equivalent of PHP explode Function
Convert Dictionary to String
Recent
Pi
NGEN Installer Class
List Element Equality
DateTime Tips and Tricks
Remove HTML Tags From String
© 2008 Sam Allen. All rights reserved.