C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Next: The two important methods are contained in a static class. This makes them easy to call from external code.
Static: In this example, we see two static conversion methods. Everything here is nicely stored in a static class because nothing saves state.
StaticDouble: Double is a type that allows more precision. Degrees may have several digits after the decimal.
DoubleAlso: The constant 5/9 is used. There are several constants here, two fractions and the number 32.
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
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