Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4617726
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:09:55+00:00 2026-05-22T02:09:55+00:00

I’m having trouble converting DMS to DD in javascript using regular expressions. I took

  • 0

I’m having trouble converting DMS to DD in javascript using regular expressions. I took this code from geoserver but it does not seem to work. I want it to be able to convert all my test cases with the correct or VERY close answer.

I really don’t care if the function is completely javascript or heavy regular expressions, i just need it to work.

You can test it on JSFIDDLE
http://jsfiddle.net/NJDp4/13/

Here are my test cases:
dmsToDeg(‘ N 03 01’ 37”’);
dmsToDeg(’03 01 37′);
dmsToDeg(’03 01’ 37” N’);
dmsToDeg(‘076 40’ 35” W’);
dmsToDeg(‘W 076 40’ 35”’);
dmsToDeg(‘N 05 11’ 17”’);

Here is my code:

function dmsToDeg (dms) {
    if (!dms) {
        return Number.NaN;
    }
    var neg = dms.match(/(^\s?-)|(\s?[SW]\s?$)/) != null ? -1.0 : 1.0;
    dms = dms.replace(/(^\s?-)|(\s?[NSEW]\s?)$/, '');
    dms = dms.replace(/\s/g, '');
    var parts = dms.match(/(\d{1,3})[.,°d ]?\s*(\d{0,2})[']?(\d{0,2})[.,]?(\d{0,})(?:["]|[']{2})?/);
    if (parts == null) {
        return Number.NaN;
    }
    // parts: 
    // 0 : degree 
    // 1 : degree 
    // 2 : minutes 
    // 3 : secondes 
    // 4 : fractions of seconde 
    var d = (parts[1] ? parts[1] : '0.0') * 1.0;
    var m = (parts[2] ? parts[2] : '0.0') * 1.0;
    var s = (parts[3] ? parts[3] : '0.0') * 1.0;
    var r = (parts[4] ? ('0.' + parts[4]) : '0.0') * 1.0;
    var dec = (d + (m / 60.0) + (s / 3600.0) + (r / 3600.0)) * neg;
    return dec;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:09:56+00:00Added an answer on May 22, 2026 at 2:09 am

    It’s not that tough, the following assumes that the correct format is passed in, you might want to do some validation of that. It also doesn’t format the output, use a generic formatting function if you need that (again, not hard to write, about 5 lines of code max).

    Likely you are feeding this into another calculation (degrees to radians conversion?) so formatting is probably unnecessary. Note that javascript numbers can’t accurately represent all decimal numbers, so be careful with rounding. The precision is sufficient for most purposes though.

    function dms2deg(s) {
    
      // Determine if south latitude or west longitude
      var sw = /[sw]/i.test(s);
    
      // Determine sign based on sw (south or west is -ve) 
      var f = sw? -1 : 1;
    
      // Get into numeric parts
      var bits = s.match(/[\d.]+/g);
    
      var result = 0;
    
      // Convert to decimal degrees
      for (var i=0, iLen=bits.length; i<iLen; i++) {
    
        // String conversion to number is done by division
        // To be explicit (not necessary), use 
        //   result += Number(bits[i])/f
        result += bits[i]/f;
    
        // Divide degrees by +/- 1, min by +/- 60, sec by +/-3600
        f *= 60;
      }
    
      return result;
    }
    
    alert( 
      dms2deg('S 023 30\'') + '\n' +      // -23.5
      dms2deg('N 023 00\' 00\"') + '\n' + //  23
      dms2deg('W 117 23\' 23.32\"')       // -117.3898111111111
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.