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

  • SEARCH
  • Home
  • 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 4612338
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:23:22+00:00 2026-05-22T01:23:22+00:00

Possible Duplicate: Regex/Javascript to transform Degrees Decimal Minutes to Decimal Degrees I have some

  • 0

Possible Duplicate:
Regex/Javascript to transform Degrees Decimal Minutes to Decimal Degrees

I have some javascript code that converts Decimal Degree Minutes to Decimal Degrees. But something is slightly screwy and am hoping someone here will know how to fix it.

How to I get this to consistently work when a user removes the ° or has extra spaces?

With the ° character it works great:

input "77° 50.51086497"
output "77.8418477495" 

input "-113° 40.54687527"
output "-113.6757812545"

However without the ° it sometimes breaks:

NOT WORKING

input "77 50.51086497"
ouput "775.0085144161667" 

WORKING

input "-113 40.54687527"
output "-113.6757812545" 

Same thing with extra spaces:

NOT WORKING

input "77   ` ` ` ` ` ` ` `50.51086497" 
output "775.0085144161667" 

WORKING

input "-113   ` ` ` ` ` ` ` `40.54687527 "
output "-113.6757812545" 

Here is my code: Please see the JSFIDDLE TO TEST

function ddmToDeg(ddm) { 
    if (!ddm) { 
        return Number.NaN; 
    } 
    var neg= ddm.match(/(^\s?-)|(\s?[SW]\s?$)/)!=null? -1.0 : 1.0; 
    ddm= ddm.replace(/(^\s?-)|(\s?[NSEW]\s?)$/,''); 
    ddm= ddm.replace(/\s/g,''); 
    var parts=ddm.match(/(\d{1,3})[.,°d]?(\d{0,2}(?:\.\d+)?)[']?/); 
        //dms.match(/(\d{1,3})[.,°d]?(\d{0,2})[']?(\d{0,2})[.,]?(\d{0,})(?:["]|[']{2})?/);
    if (parts==null) { 
        return Number.NaN; 
    } 
    // parts: 
    // 0 : degree 
    // 1 : degree 
    // 2 : minutes 


    var d= (parts[1]?         parts[1]  : '0.0')*1.0; 
    var m= (parts[2]?         parts[2]  : '0.0')*1.0; 
    var dec= (d + (m/60.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-22T01:23:23+00:00Added an answer on May 22, 2026 at 1:23 am

    I answered this on the original question: Regex/Javascript to transform Degrees Decimal Minutes to Decimal Degrees

    Solution posted to your fiddle: http://jsfiddle.net/NJDp4/6/

    dmsToDeg: function(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?)$/,''); 
        var parts=dms.match(/(\d{1,3})[.,°d ]?\s*(\d{0,2}(?:\.\d+)?)[']?/); 
            if (parts==null) { 
                return Number.NaN; 
            } 
            // parts: 
            // 0 : degree 
            // 1 : degree 
            // 2 : minutes 
    
    
            var d= (parts[1]?         parts[1]  : '0.0')*1.0; 
            var m= (parts[2]?         parts[2]  : '0.0')*1.0; 
            var dec= (d + (m/60.0))*neg; 
            return dec; 
        }
    

    The reason this was working for “-113 40.54687527” but not for “77 50.51086497” is that the code was ripping out spaces and the sign (leaving “11340.54687527” and “7750.51086497”) and then grabbing the first 3 digits to use as degrees (so “113” and “775”). I’ve modified this so that it no longer strips out spaces before grabbing the two numbers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Escape string for use in Javascript regex I have a msg like
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: Undefined Symbol ___gxx_personality_v0 on link I have a problem with the following
Possible Duplicate: How to run a database script file from Delphi? I have a
Possible Duplicate: Bind textbox to 'enter' key I have a search text box. I
Possible Duplicate: Image Button in BlackBerry Is there a way to have a picture
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be

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.