using System;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;


namespace MetricFunctions
{

public class MetricConverter
{
  const double KilometersPerMile = 1.609347;
  const double KilogramsPerPound = 0.4535924;

  public static SqlDecimal FahrenheitToCentigradeDec(SqlDecimal v)
  {
     return ((v-32)/9)*5;
  }

  [SqlFunction(DataAccess=DataAccessKind.None,IsDeterministic=true,IsPrecise=true)]
  public static SqlDouble FahrenheitToCentigrade(SqlDouble v)
  {
	  if (v.IsNull == true)
		  return 0;
	  else
		  return ((v-32)/9)*5;
  }
  public static SqlDouble CentigradeToFahrenheit(SqlDouble v)
  {
	  return (v*9)/5+32;
  }
  public static SqlDouble MilesToKilometers(SqlDouble v)
  {
	  return v*KilometersPerMile;
  }
  public static SqlDouble KilometersToMiles(SqlDouble v)
  {
	  return v/KilometersPerMile;
  }
  public static SqlDouble PoundsToKilograms(SqlDouble v)
  {
	  return v*KilogramsPerPound;
  }
  public static SqlDouble KilogramsToPounds(SqlDouble v)
  {
	  return v/KilogramsPerPound;
  }
}

}
