1. ## DD to DMS

Can i know how to write C code for converting longitude and latitude from DD to DMS and also the cardinal direction? I have no idea on it. 2. Do you have some test data and results you can share?  4. I've finally got to the point where I no longer trust floating point - because some things are a little unpredictable, and "float" is only accurate to 25 bits, and just degrees, minutes and seconds can use up about 20 of those.

So I would suggest you:

- first ensure that the input is in the range expected +/-90.0 for latitude, +/-180.0 for longitude. fmod() might come in handy here.

- convert your number to an 32-bit integer, with the unit of 1000 arc-seconds - multiply your floating point number by (60*60*1000) and add 0.5, and assign it to an 32-bit integer. 180.0 degrees will become 648,000,000 - and that fits fine in a 32-bit integer.

- The you can use the '%' and '/' operators to extract:

-- thousandths of arc seconds (use % and / by 1000)
-- arc seconds (use % and / by 60)
-- arc minutes (use % and / by 60)
-- degrees (the remainder left over)

The sign of the degrees will tell you if it is North, South (for latitude) or East or West (for longitude). 5. Originally Posted by hamster_nz I've finally got to the point where I no longer trust floating point - because some things are a little unpredictable, and "float" is only accurate to 25 bits, and just degrees, minutes and seconds can use up about 20 of those.
float has 24 bits of precision, not 25... But you can use double (53 bits). In an Intel or ARM processor there's no difference in performance (only double has "double" the size, in comparison with float, and depending on the structures you can get a more severe cache miss effects).

Anyway... I don't trust floating point as well and prefer using integers. Not because of precision, but because of accuracy. Some simple values cannot be exactly represented in floating point, such as 0.1. 6. ## from DD to DMS
Code:
```#include <stdio.h>

double DD = 0.0, seconds, remain ;
int degrees = 0, minutes = 0;

int main() {

DD = 13.5423;

degrees = (int)DD;
remain = ((DD-degrees)*60);
minutes = (int)(remain);
seconds = ((remain-minutes)*60);

printf( "DD:\t %f \n", DD );
printf( "DMS:\t %i° %i' %.2f\" \n", degrees, minutes, seconds);

return 0;
}``` Popular pages Recent additions cardinal, direction, dms, idea, longitude 