Home Articles Components Code Snippets Links

# C# Convert Radians and Degrees

## C# Trigonometry

Dealing with C#.NET angles can be bit confusing. when you call Math.Cos(), Math.Sin(), or Math.Tan() in C#, the built-in math library asks for an input as a double. If you look in most calculators, these three trigonometric functions can be used in two angle modes, degrees and radians.

It can be a bit confusing at first which angle mode the C#.NET is using, but the answer is radians.

Why would C# not come with support for angles in degrees? Well because it turns out converting between radians and degrees is not all that difficult. We can use simple cross-multiplication in C# to convert the angles.

In order to get the functions to work with degree angles, we just need to convert the angle in degrees to radians before we use it on the C# Math functions.

Basically we need to know one thing, 180 degrees equals PI radians. With that we can set up a simple ratio, cross multiply, and divide to get any degree's radian equivalent.

The C# function to convert degrees to radians comes out to look like this:

```private double DegreeToRadian(double angle)
{
return Math.PI * angle / 180.0;
}```

So for example we could write the following C# code:

```double angle = Math.Cos(DegreeToRadian(45));
```

Which would give the Cos of a 45 degree angle. Although it is not necessary, you might find you need to convert radians to degrees. In you do, here is a function to convert radians to degrees based on the same cross-multiplication rules:

```private double RadianToDegree(double angle)
{
return angle * (180.0 / Math.PI);
}```

Both C# functions use double types to keep the division from rounding too early. Just something to keep in mind.

Converting between radians and degrees comes out to be extremely simple. So simple in fact, that the C# math library simply handles radian angles and leaves the conversion to you.

Back to C# Article List

Other C# Articles