Imports System
Imports Microsoft.SqlServer.Server
Imports System.Data.SqlTypes
Imports System.Reflection
Imports System.Runtime.CompilerServices

'To sign this assembly, compile it from the command-line with vbc
' and uncomment the following line
'<Assembly: AssemblyKeyFile("pubpriv.snk")> 
<Assembly: AssemblyVersion("1.0.0.0")> 

Namespace MetricFunctions

    Public Class MetricConverter
        Private Const KilometersPerMile As Double = 1.609347
        Private Const KilogramsPerPound As Double = 0.4535924

        Public Shared Function FahrenheitToCentigradeDec(ByVal v As SqlDecimal) As SqlDecimal
            Return ((v - 32) / 9) * 5
        End Function

        <SqlFunction(DataAccess:=DataAccessKind.None, IsDeterministic:=True, IsPrecise:=True)> _
  Public Shared Function FahrenheitToCentigrade(ByVal v As SqlDouble) As SqlDouble
            If v.IsNull = True Then
                Return 0
            Else
                Return ((v - 32) / 9) * 5
            End If
        End Function
        Public Shared Function CentigradeToFahrenheit(ByVal v As SqlDouble) As SqlDouble
            Return (v * 9) / 5 + 32
        End Function
        Public Shared Function MilesToKilometers(ByVal v As SqlDouble) As SqlDouble
            Return v * KilometersPerMile
        End Function
        Public Shared Function KilometersToMiles(ByVal v As SqlDouble) As SqlDouble
            Return v / KilometersPerMile
        End Function
        Public Shared Function PoundsToKilograms(ByVal v As SqlDouble) As SqlDouble
            Return v * KilogramsPerPound
        End Function
        Public Shared Function KilogramsToPounds(ByVal v As SqlDouble) As SqlDouble
            Return v / KilogramsPerPound
        End Function
    End Class

End Namespace