TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Convert Degrees Celsius to Fahrenheit

These C# methods do numeric conversions for degrees Celsius and Fahrenheit.

Convert degrees. Degrees Celsius can be converted to degrees Fahrenheit.

We do this with code written in the C# programming language. It is more convenient to adapt proven code than write the same code again. We compare the results to Google to check them.

Example input

100 °C
212 °F
50  °C
122 °F

Example output

212 °F
100 °C
122 °F
50  °C

Example. Here are some simple methods that perform Celsius and Fahrenheit conversions. You can convert Celsius to Fahrenheit and back again using the constant 5/9. The CLR will compile the expression into a constant.

DivideJIT

Next: The two important methods are contained in a static class. This makes them easy to call from external code.

C# program that converts degrees

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);
	}
    }
}

Output

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

In this example, we see two static conversion methods. Everything here is nicely stored in a static class because nothing saves state. Double is a type that allows more precision. Degrees may have several digits after the decimal.

Double Type

Also: The constant 5/9 is used. There are several constants here, two fractions and the number 32.

Note: These match Google's conversion utilities, which likely has the same algorithm.

Google search

Body temperature. 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.

C# program that converts human temperature

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"));

    }
}

Output

98.6, 37.0

Summary. We looked at how you can convert temperatures in degrees Celsius and degrees Fahrenheit using methods written in the C# programming language. We tested two methods against Google's conversion function.

And: Scientific applications may need more elaborate methods, but these suffice normally.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf