I was recently working on a thermometer control and decided to write a temperature class to go along with it. What I found interesting is that there are temperature systems that I had never even heard of let alone used (namely Reaumer and Rankine). I'm from Canada and I've heard of some pretty nasty temperatures, but these were still a suprise to me. :-) Anyway I wrote the class in C# and thought others might find some use in it. Here it is:
using System;
namespace ThermometerDemo
{
/// <summary>
/// Summary description for Temperature.
/// </summary>
public class Temperature
{
public enum SystemOfMeasure { Fahrenheit = 0, Celsius, Kelvin, RanKine, Reaumer };
private SystemOfMeasure _systemOfMeasure;
public SystemOfMeasure Scale
{
get { return _systemOfMeasure; }
set { _systemOfMeasure = value; }
}
private double _temperatureKelvin;
public double Value
{
get { return Temperature.Convert( _temperatureKelvin, SystemOfMeasure.Kelvin, _systemOfMeasure ); }
set { this._temperatureKelvin = Temperature.Convert( value, _systemOfMeasure , SystemOfMeasure.Kelvin ); }
}
public double AbsoluteZero
{
get { return Temperature.Convert( 0F, SystemOfMeasure.Kelvin, _systemOfMeasure ); }
}
#region Construction
public Temperature() : this( SystemOfMeasure.Kelvin, 298.15F )
{
// SI temperature at standard state in Kelvin.
}
public Temperature( SystemOfMeasure systemOfMeasure ) : this( systemOfMeasure, 298.15F )
{
// SI temperature at standard state.
}
public Temperature( double temperature ) : this( SystemOfMeasure.Kelvin, temperature )
{
}
public Temperature( SystemOfMeasure systemOfMeasure, double temperature )
{
_systemOfMeasure = systemOfMeasure;
Value = temperature;
}
#endregion Construction
# region Public Methods
public static double Convert( double temperature, SystemOfMeasure originSystem, SystemOfMeasure targetSystem )
{
// Convert to Kelvin.
double temperatureKelvin;
switch( originSystem )
{
case SystemOfMeasure.Fahrenheit:
temperatureKelvin = ( 5F / 9F ) * ( temperature - 32 ) + 273.15F;
break;
case SystemOfMeasure.Celsius:
temperatureKelvin = temperature + 273.15F;
break;
case SystemOfMeasure.Kelvin:
temperatureKelvin = temperature;
break;
case SystemOfMeasure.Reaumer:
temperatureKelvin = ( 5F / 9F ) * temperature + 273.15F;
break;
case SystemOfMeasure.RanKine:
temperatureKelvin = temperature / 1.8F;
break;
default:
throw new ArgumentOutOfRangeException( "originSystem", originSystem, "Unsupported origin system of measure");
}
// Convert to the new system.
double resultTemperature;
switch( targetSystem )
{
case SystemOfMeasure.Fahrenheit:
resultTemperature = 32F + ( 1.8F ) * ( temperatureKelvin - 273.15F );
break;
case SystemOfMeasure.Celsius:
resultTemperature = temperatureKelvin - 273.15F;
break;
case SystemOfMeasure.Kelvin:
resultTemperature = temperatureKelvin;
break;
case SystemOfMeasure.Reaumer:
resultTemperature = ( 1.8F ) * ( temperatureKelvin - 273.15F );
break;
case SystemOfMeasure.RanKine:
resultTemperature = temperatureKelvin * 1.8F;
break;
default:
throw new ArgumentOutOfRangeException( "originSystem", originSystem, "Unsupported target system of measure");
}
return resultTemperature;
}
public static string GetUnitAbbreviation( SystemOfMeasure systemOfMeasure )
{
switch( systemOfMeasure )
{
case SystemOfMeasure.Fahrenheit:
return "°F";
case SystemOfMeasure.Celsius:
return "°C";
case SystemOfMeasure.Kelvin:
return "K";
case SystemOfMeasure.Reaumer:
return "°Re";
case SystemOfMeasure.RanKine:
return "°Ra";
default:
throw new ArgumentOutOfRangeException( "systemOfMeasure", systemOfMeasure, "Unsupported system of measure");
}
}
#endregion Public Methods
}
}
eHowitzer