Let us first consider the positive value case. I will pick a random decimal value of 12.34567°
If we take the integer value of that, we obtain 12°, with a remainder of 0.33567°. So now we know D, of DMS.
To determine M, take the integer value of ((12.34567 - 12.00000) * 60). So we have integer((0.34567)*60) = integer (20.7402) = 20 minutes, with a remainder of 0.7402 minutes. So M=20.
To determine S is a bit trickier. Let's assume you want the value rounded to the nearest second. Find the round ((12.34567 - 12.00000 - 20/60)*3600). That is from the original value of 12.34567°, subtract the whole number of degrees and subtract the whole number of minutes, converted to degrees by dividing that by 60. This will yield the number of seconds, in units of degrees, so multiplying by 3600 converts that intermediate result to units of decimal seconds. To resume the example, we have round ((0.0123366)*3600) = round (44.412) = 44 seconds. It rounded down, therefore S=44
Thus we have
12.34567° = 12°20'44"
That's decimal degrees to DMS for the positive case.
That's more than enough head start on the negative case. It works in a similar fashion, but be careful. Take the integer value of -54.321°. Do you get -55° or -54°? We want -54. Be careful when determining the remainder, as we want -.321. Make sure your signs are appropriate.
I converted all the way to rounded seconds. Suppose you wanted a CN result. That would be degrees and decimal minutes, rounded to the nearest tenth of a minute. That is, D.M.m.
The first step is the same, integer(12.34567°) = 12.°
The second step is a bit different. Take the remainder of 0.34567 and multiply by 600. That is, there are 600 tenths of a minute per degree. 0.34567*600=207.402 tenths of a minute. Find the round(207.402) and we get 207. Now divide by 10, obtaining 20.7 decimal minutes.
Again, I will leave the exercise to you Tony, to evaluate the negative case. It's no fun if I just tell you the answer, you should have to work at it a bit. May I suggest you try to write this in MS Excel spreadsheet?