Created
January 10, 2017 09:54
-
-
Save johanmynhardt/64c23412c29690e7351cb6a378ec93ca to your computer and use it in GitHub Desktop.
Decimal to DMS converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dd2dms = (latOrLong) => { | |
latOrLong = (typeof latOrLong === 'number') ? ''+latOrLong : latOrLong; //convert to string | |
const nan2S = (input) => (isNaN(input) || 'NaN' === input) ? '0' : `${input}`; | |
const result = { | |
sign: '', | |
deg: '0', | |
min: '0', | |
sec: '0' | |
}; | |
// check for `-` | |
let value; | |
if (latOrLong.substr(0, 1) === "-") { | |
value = latOrLong.substr(1, latOrLong.length - 1); | |
result.sign = '-'; | |
} else { | |
value = latOrLong; | |
} | |
// degrees = degrees | |
let values = value.split("."); | |
result.deg = nan2S(values[0]); | |
// * 60 = mins | |
let remainder = ("0." + values[1]) * 60; | |
let minuteValues = remainder.toString().split("."); | |
result.min = nan2S(minuteValues[0]); | |
// * 60 again = secs | |
let minuteRemainder = ("0." + minuteValues[1]) * 60; | |
result.sec = nan2S(Math.round(minuteRemainder)); | |
return result; | |
}; | |
const dd2dmsToString = (latOrLon) = `${latOrLon.sign}${latOrLon.deg}° ${latOrLon.min}' ${latOrLon.sec}"`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment