Convert Degrees Celsius to FahrenheitPerform numeric conversions for degrees Celsius and Fahrenheit.
C#
Convert degrees. Degrees Celsius can be converted to degrees Fahrenheit. It is more convenient to adapt proven code than write the same code again.
Science class. We can convert temperatures with the constant 5/9. This can be done in C# programs as well as with a calculator or Internet search.
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.
Divide
Next The 2 important methods are contained in a static class. This makes them easy to call from external code.
static
Detail Double is a type that allows more precision. Degrees may have several digits after the decimal.
double
Also The constant 5/9 is used. There are several constants here, two fractions and the number 32.
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); } } }
100 = 212 212 = 100 50 = 122 122 = 50
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 degrees F. Note that this varies between people.
Note To use this program, make sure to paste in the ConvertTemp class in the same project.
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. We looked at how you can convert temperatures in degrees Celsius and degrees Fahrenheit. Scientific applications may need more elaborate methods.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.