# Convert DegMinSec to Decimal Degrees

• 11-20-2007
JacquesLeJock
Convert DegMinSec to Decimal Degrees
Convert DegMinSec to Decimal Degrees

Hello,

I once again found a little time to toy with C Programming.
I have applied the helpful advice from this message board since my last post.
I used the utility on http://www.fcc.gov/mb/audio/bickel/DDDMMSS-decimal.html
to check my output.
I was pleased that my program output matched that of the utility except on
my final test input of 1.0101.
Please see the comments at the end of my program for details.

Code:

```/* Name: TODECDEG.C */ /* Purpose: Convert Degrees Minutes Seconds to Decimal Degrees */ /* Author: JacquesLeJock */ /* Date: November 19, 2007 */ /* Compiler: Pacific C for MS-DOS, v7.51 */ #include <stdio.h> main() { double deg_min_sec = 0, decimal_deg = 0; double mind = 0, secd = 0; int deg = 0, min = 0, sec = 0; printf("Enter Degrees Minutes Seconds: "); scanf("%lf", &deg_min_sec); deg = deg_min_sec; /* conversion during assignment */ min = (deg_min_sec - deg) * 100; sec = ((deg_min_sec - deg) * 100 - min) * 100; mind = (double) min / 60; /* http://c-faq.com/expr/truncation1.html */ secd = (double) sec / 3600; decimal_deg = deg + mind + secd; printf("Decimal Degrees: %f\n\n", decimal_deg); return 0; } /* Sample output: Enter Degrees Minutes Seconds: 1.0101 Decimal Degrees: 1.016667 Press any key to continue ... */ /* Note: This conversion does not agree with http://www.fcc.gov/mb/audio/bickel/DDDMMSS-decimal.html. Their utility yields 1.016944. Why? */```
Any help would be greatly appreciated!

Regards,

Jacques
• 11-20-2007
NeonBlack
rounding errors are probably causing sec to be something like 0.99999999999998
which is assigned to an int as 0 instead of 1.
• 11-21-2007
Salem
Consider using a single multiply by 10000 (not *100 then *100).
Then you can stick to only using integer arithmetic to extract all the information you need.
• 11-21-2007
JacquesLeJock
Thanks for the reply NeonBlack and Salem!