if v is the input value(double), define pi2=2*Math.PI

then v%pi2 will give you remainder.

i.e.
3.2 % 2.0 will give you 1.2
6.5 % 2.0 will give you 1.5
6.29 % 6.28 will give you 0.01
6.27 % 6.28 will give you 6.27
12.57 % 6.28 will give you 0.01

If the remainder is less than 0.01 or larger than 6.27 will be good value for your case.
You do not need to use loop.

I hope this will help.