Skip to content

Instantly share code, notes, and snippets.

@mbykovskyy
Created August 22, 2014 03:31
Show Gist options
  • Save mbykovskyy/1c67b0b4ba8da9972488 to your computer and use it in GitHub Desktop.
Save mbykovskyy/1c67b0b4ba8da9972488 to your computer and use it in GitHub Desktop.
Convert decimal degrees to degrees minutes seconds.
/**
* Converts decimal degrees to degrees minutes seconds.
*
* @param dd the decimal degrees value.
* @param isLng specifies whether the decimal degrees value is a longitude.
* @return degrees minutes seconds string in the format 49°15'51.35"N
*/
function convertToDms(dd, isLng) {
var dir = dd < 0
? isLng ? 'W' : 'S'
: isLng ? 'E' : 'N';
var absDd = Math.abs(dd);
var deg = absDd | 0;
var frac = absDd - deg;
var min = (frac * 60) | 0;
var sec = frac * 3600 - min * 60;
// Round it to 2 decimal points.
sec = Math.round(sec * 100) / 100;
return deg + "°" + min + "'" + sec + '"' + dir;
}
@chananaimen
Copy link

מה אומר קוד זה
var dir = dd < 0
? isLng ? 'W' : 'S'
: isLng ? 'E' : 'N';

@ijl20
Copy link

ijl20 commented Feb 23, 2024

BUG in this code: convertToDms(36.183333333333, true) = 36°10'60"E

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment