I was recently using Google Maps API geo-location lookups to get the longitude and latitude of an address entered by the user. I wanted to find the distance between a two co-ordinates using the Haversine Formula. To do this I needed to convert my lat/lng co-ordinates into radians. This seemed like an excellent opportunity to create a new extension method to add to Storm‘s library of re-usable code.
The maths to convert an angle from degrees to radians is really quite simple:
(pi / 180) * angle
We can take this simple equation and create either a simple function, or a convenient extension method.
C# Function
public double ConvertToRadians(double angle)
{
return (Math.PI / 180) * angle;
}
C# Extension Method
/// <summary>
/// Convert to Radians.
/// </summary>
/// <param name="val">The value to convert to radians</param>
/// <returns>The value in radians</returns>
public static class NumericExtensions
{
public static double ToRadians(this double val)
{
return (Math.PI / 180) * val;
}
}


