Because even the Microsoft code is buggy, it may be useful to provide correct pseudocode for the conversion.
To convert decimal degrees x to degrees (d), minutes(m), and (decimal) seconds (s), do the following:
Declare d, m as integer, s as float
If x < 0, then sign = -1 else sign = +1
Let y = Abs(x) ' Work with positive values only.
Let d = Int(y) ' Whole degrees. Floor() is ok too.
Let z = 60*(y - d) ' The fractional degrees, converted to minutes.
Let m = Int(z) ' Whole minutes.
Let s = 60*(z - m) ' The fractional minutes, converted to seconds.
Assert sign*(((s/60) + m)/60 + d) == x ' This confirms a correct result.
Return (sign*d, m, s)
Instead of returning a signed degree, you can indicate N/S or E/W at the end:
If x is a latitude, then
If sign == -1 then hemisphere = "S" else hemisphere = "N"
Else {x is a longitude}
If sign == -1 then hemisphere = "W" else hemisphere = "E"
End if
Return (d, m, s, hemisphere)
If you like, you can round s to an integer and format the results to match the form given in the question.