DMS to decimal degrees

HTML for this example

Select the code and use your copy shortcut. Changes in this preview are temporary.

<!doctype html><html lang="en-US"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>DMS to decimal degrees</title><style>*{box-sizing:border-box}html{color-scheme:light}body{margin:0;background:var(--bg,#eef1f4);color:var(--ink,#233449);font:16px/1.5 system-ui,-apple-system,Segoe UI,sans-serif}main{max-width:1040px;margin:auto;padding:26px 28px 44px}header{margin-bottom:20px}h1{font-size:clamp(26px,4vw,36px);letter-spacing:-.035em;line-height:1.15;margin:5px 0 10px}h2{font-size:21px;line-height:1.3;margin:0 0 15px}h3{font-size:17px;margin:0 0 9px}p{margin:8px 0 16px}.kicker{font-size:12px;text-transform:uppercase;letter-spacing:.12em;font-weight:750;color:var(--accent,#315b87)}.muted,small{color:#556473}.panel{margin:20px 0;padding:24px;border:1px solid var(--line,#ccd5df);border-radius:10px;background:#fff}.hero{padding:24px;background:var(--hero,#183b5c);color:#fff;border-radius:12px;margin:18px 0}.hero .kicker,.hero small{color:#d9e5ef}.big{font-size:clamp(32px,5vw,52px);line-height:1.15;font-weight:750;letter-spacing:-.035em;overflow-wrap:anywhere;margin:10px 0 15px}.row{display:flex;flex-wrap:wrap;gap:15px;align-items:end}.row>label{flex:1;min-width:140px}.grid{display:grid;grid-template-columns:1fr 1fr;gap:20px}.thirds{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:15px}label{display:block;font-size:14px;font-weight:650}input,select,textarea{font:inherit;font-size:16px;color:inherit;display:block;width:100%;margin-top:6px;padding:11px 12px;border:1px solid #a9b4c1;border-radius:6px;background:white;min-width:0}input[type=checkbox]{display:inline;width:auto;margin:0 8px 0 0;accent-color:var(--accent,#315b87)}textarea{min-height:100px;resize:vertical}button{font:inherit;font-weight:650;min-height:44px;border:1px solid var(--accent,#315b87);border-radius:6px;background:var(--accent,#315b87);color:white;padding:10px 16px;cursor:pointer}button.secondary{background:white;color:var(--accent,#315b87)}button:disabled{opacity:.5;cursor:default}button[aria-pressed=true]{box-shadow:0 0 0 3px #eab655}button:hover:not(:disabled){filter:brightness(.94)}:focus-visible{outline:3px solid #e5a72f;outline-offset:3px}.actions{display:flex;flex-wrap:wrap;gap:10px;margin:18px 0 0}[hidden]{display:none!important}.error{color:#a3232a;font-weight:650}.error:not(:empty){padding:12px;background:#fff0ed;border-left:4px solid #ab3439;margin:14px 0}.note{padding:14px;background:#edf2f6;border-radius:6px;margin:14px 0}.mono,pre{font-family:ui-monospace,Consolas,monospace;font-variant-numeric:tabular-nums}pre{white-space:pre-wrap;overflow-wrap:anywhere;font-size:14px;line-height:1.7;margin:0}.mono{overflow-wrap:anywhere}table{width:100%;border-collapse:collapse;table-layout:fixed;font-size:14px}th,td{text-align:left;vertical-align:top;overflow-wrap:anywhere;padding:11px 8px;border-bottom:1px solid #d4dce4}th{font-size:12px;text-transform:uppercase;letter-spacing:.035em;color:#536372}th:first-child,td:first-child{padding-left:0}.tag{display:inline-block;padding:4px 8px;border:1px solid currentColor;border-radius:4px;font-size:12px;font-weight:750}.rule{border-top:1px solid #cbd4dd;padding-top:18px}.line{height:30px;border-bottom:1px solid #aab5bf}.stat{font-size:34px;line-height:1.15;font-weight:750}.stat small{font:14px system-ui;display:block;margin-top:8px}details{margin:20px 0;border-top:1px solid #cbd4dd;padding:18px 0}summary{font-weight:700;cursor:pointer}details p{margin-top:15px}a{color:var(--accent,#315b87)}footer{font-size:13px;color:#556473;margin-top:25px}fieldset{min-width:0;border:1px solid #c8d2db;border-radius:7px;padding:18px;margin:15px 0}legend{font-weight:750;padding:0 6px}@media(max-width:720px){main{padding:20px}.panel,.hero{padding:20px}.thirds{gap:10px}}@media(max-width:540px){main{padding:16px}.grid,.thirds{grid-template-columns:1fr}.panel,.hero{padding:18px}.row{gap:12px}.row>label{min-width:100px}h1{font-size:27px}th,td{padding:10px 5px;font-size:13px}.big{font-size:37px}}.hero{border-left:6px solid #b9c6ef}.hero .big{font-family:Georgia,serif}</style></head><body style="--bg:#f0f1f8;--accent:#4c5689;--hero:#303963"><main><header><div class="kicker">Working example</div><h1>DMS to decimal degrees</h1><p class="muted">Apply one sign to the complete degree, minute, and second magnitude.</p></header><section id="result" class="hero" aria-live="polite"><div class="kicker">General angle · decimal degrees</div><div class="big" id="angle"></div><p id="formula" class="mono"></p></section><p id="error" class="error" role="alert"></p><form id="form" class="panel" novalidate><div class="row"><label for="sign">Whole-value sign<select id="sign"><option value="+">Positive (+)</option><option value="-">Negative (−)</option></select></label><label for="degrees">Degrees · integer<input id="degrees" value="12" inputmode="decimal" autocomplete="off"></label><label for="minutes">Minutes · 0–59<input id="minutes" value="15" inputmode="decimal" autocomplete="off"></label><label for="seconds">Seconds · 0 to <60<input id="seconds" value="30" inputmode="decimal" autocomplete="off"></label></div><label for="precision" style="margin-top:18px">Display decimal places<select id="precision"><option value="9">9 places</option><option value="6">6 places</option></select></label><div class="actions"><button>Convert angle</button><button type="button" id="nearzero" class="secondary">Try −0° 30′ 0″</button></div></form><section class="panel"><h2>Calculation record</h2><pre id="trace"></pre></section><section class="panel"><h2>Source-format worksheet</h2><div class="grid"><label for="source">Original source text<input id="source" value="Synthetic angle: 12° 15′ 30″" inputmode="text" autocomplete="off"></label><label for="reference">Source reference<input id="reference" value="Example A · authored fixture" inputmode="text" autocomplete="off"></label></div><p class="note"><b>Datum: unknown.</b> Decimal formatting does not transform a datum or improve location accuracy.</p><p id="sourceRecord"></p></section><details><summary>Parser and interpretation limits</summary><p>Sign is separate. Degrees must be a nonnegative integer up to 1 billion; minutes an integer 0–59; seconds a nonnegative dot-decimal below 60 with up to nine places. Blank is missing. Values of 60 are rejected rather than silently carried. Displays use half-up rounding of magnitude.</p><p>This is a general angle, with no inferred hemisphere, latitude/longitude, map, or GPS record. Native source text and metadata remain authoritative.</p></details><footer>Local example · No information is sent or saved.</footer></main><script>const $=id=>document.getElementById(id);const esc=s=>String(s).replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));function gcd(a,b){return b?gcd(b,a%b):a}function rat(n,d=1n){const g=gcd(n,d);return {n:n/g,d:d/g}}function mul(a,b){return rat(a.n*b.n,a.d*b.d)}function div(a,b){return rat(a.n*b.d,a.d*b.n)}function rounded(r,p=6){const scale=10n**BigInt(p),q=(r.n*scale*2n+r.d)/(2n*r.d),s=q.toString().padStart(p+1,'0');return p?s.slice(0,-p)+'.'+s.slice(-p):s}function show(r,unit=''){if(r.n>0n&&r.n*1000000n<r.d)return '<0.000001 '+unit;return '≈ '+rounded(r,6).replace(/([.][0-9]*?)0+$/,'$1').replace(/[.]$/,'')+' '+unit}function decimal(s,name,max=1000000,places=6){if(!new RegExp('^[0-9]+(?:[.][0-9]{1,'+places+'})?$').test(s)||s.length>35)throw Error(name+': enter a nonnegative dot-decimal number, up to '+places+' decimal places.');let [a,b='']=s.split('.');const r=rat(BigInt(a+b),10n**BigInt(b.length));if(r.n>BigInt(max)*r.d)throw Error(name+': maximum is '+max+'.');return r}function whole(s,name,max){if(!/^[0-9]+$/.test(s)||s.length>15||Number(s)>max)throw Error(name+': enter an integer from 0 to '+max+'.');return Number(s)}function onForm(fn,clear){$('form').addEventListener('submit',e=>{e.preventDefault();fn()});$('form').addEventListener('input',clear);$('form').addEventListener('change',clear)}

function dms(d,m,s,sign){const D=whole(d,'Degrees',1000000000),M=whole(m,'Minutes',59),S=decimal(s,'Seconds',60,9);if(S.n>=60n*S.d)throw Error('Seconds: must be less than 60.');if(!['+','-'].includes(sign))throw Error('Select a whole-value sign.');return {magnitude:rat(BigInt(D*3600+M*60)*S.d+S.n,3600n*S.d),negative:sign==='-',D,M,S}}
function clear(){ $('result').hidden=true;$('trace').textContent='';$('error').textContent='';}function metadata(){ $('sourceRecord').textContent='Original notation: '+$('source').value+' | Source reference: '+$('reference').value+' | Datum: unknown';}function run(){try{const r=dms($('degrees').value,$('minutes').value,$('seconds').value,$('sign').value),p=Number($('precision').value);if(![6,9].includes(p))throw Error('Unsupported display precision.');$('result').hidden=false;$('error').textContent='';const prefix=r.negative&&r.magnitude.n?'-':'';$('angle').textContent='≈ '+prefix+rounded(r.magnitude,p)+'°';$('formula').textContent=(r.negative?'−':'')+'('+r.D+' + '+r.M+'/60 + '+$('seconds').value+'/3600)';$('trace').textContent='Minutes contribution: '+r.M+'/60\nSeconds contribution: '+$('seconds').value+'/3600\nWhole magnitude (exact): '+r.magnitude.n+' / '+r.magnitude.d+' degrees\nSign applied to the entire magnitude: '+(r.negative?'negative':'positive')+'\nDisplay rounded to '+p+' places: '+$('angle').textContent+'\nDatum: unknown; formatting only.';metadata();}catch(e){clear();$('error').textContent=e.message}}onForm(run,clear);['source','reference'].forEach(x=>$(x).addEventListener('input',metadata));$('nearzero').onclick=()=>{$('sign').value='-';$('degrees').value='0';$('minutes').value='30';$('seconds').value='0';$('source').value='Synthetic angle: −0° 30′ 0″';$('reference').value='Near-zero sign fixture';run()};run();</script>
<script>
// Local calculation controls: no native form submission or sandbox permissions required.
(()=>{const form=document.getElementById('form');if(!form)throw Error('Calculation form missing');const dispatch=()=>form.dispatchEvent(new Event('submit',{bubbles:true,cancelable:true}));const buttons=[...form.querySelectorAll('button')].filter(button=>button.type==='submit');if(buttons.length!==1)throw Error('Expected one calculation control');for(const button of buttons){button.type='button';button.addEventListener('click',dispatch);}form.addEventListener('keydown',event=>{if(event.key==='Enter'&&!event.isComposing&&['INPUT','SELECT'].includes(event.target.tagName)){event.preventDefault();dispatch();}});})();
</script>
</body></html>