What have you tried so far?
If you're already reading the direction and getting a result in degrees, take that result and divide by 90, and then round() to the nearest integer. This will give you 0, 1, 2, 3 or 4. Then multiply by 90 to get 0, 90, 180, 270 or 360. If your code can't handle a result of 0 or 360, change it to be whichever one your code is ok with.