Converting three axis magnetometer to Degrees
The answer given by Dave X, namely

az=90-atan(y/x)*180/pi


is on the right track. However, it cannot work as is for two reasons:


The formula fails badly whenever x = 0,
It gives the same azimuth for x = y = 1 and for x = y = −1. In other
words, it cannot differentiate between north-east and south-west.


The formula could be fixed by replacing atan(y/x) by a function that
returns:


atan(y/x) if x > 0
π/2 if x = 0 and y > 0
−π/2 if x = 0 and y &lt; 0
atan(y/x) + π if x &lt; 0 and y ≥ 0
atan(y/x) − π if x &lt; 0 and y &lt; 0


That would be essentially a two-argument, “quadrant-aware” version of
the arctangent, yielding results in (-π,&nbsp;π] instead of
(−π/2,&nbsp;π/2).

Fortunately, such function already exists. It is a very standard
function of the C library and is called atan2(). Thus, the correct
formula is

az = 90 - atan2(y, x) * 180 / M_PI;


Now, since atan(y/x) = π/2 − atan(x/y), the formula above can be
simplified into

az = atan2(x, y) * 180 / M_PI;


This formula gives the direction of the (x, y) vector counted clockwise
from the y axis. It is likely that the OP wants instead to know the
heading of some kind of vehicle. The correct formula depends then on how
the magnetometer has been mounted relative to the vehicle. If we assume
that the x axis of the magnetometer points forward and the y axis points
to the left, then:


the vehicle is heading north (0° magnetic heading) when the horizontal
component of the magnetic field is along +x
the vehicle is heading east (90° magnetic heading) when the horizontal
component of the magnetic field is along +y


From here, it follows that the vehicle heading is

heading = atan2(y, x) * 180 / M_PI;


Obviously, the magnetic declination has to be added if one is
interested in the true (rather than magnetic) heading.