DEV Community

Cover image for Converting DMS (Degrees, Minutes, Seconds) to Decimal & Decimal to DMS
Reagan Scofield
Reagan Scofield

Posted on

Converting DMS (Degrees, Minutes, Seconds) to Decimal & Decimal to DMS

Everytime I am looking for npm package that will meet my requirements and end up by not finding any package on npm site I always try to write it myself and then publish it to npm so last week I was working on the features which need to receive a DMS (Degrees Minutes and Seconds) value and then convert it to Decimal and return the decimal so I wrote this package which does all the case the first function does convert DMS to Decimal and the second function does convert Decimal to DMS.

The Dms2Decimal function take 4 parameters degrees, minutes, second as an integer and direction as a string, it will return 0 if not found and return decimal numbers if found.

The Decimal2DMS does take 2 parameters decimal as a float and type as a string the type value need to be latitude or longitude and this will return null if not found or it will return a string in this format 23°34'5"N if found.

Implementation

    import { Decimal2DMS, DMS2Decimal } from 'dms-to-decimal';

    // Converting Degrees Minutes Seconds to Decimal 
    const degree = 46;
    const minutes = 59;
    const seconds = 5;
    const direction = 'N';

    const converDms2Decimal = DMS2Decimal(degree, minutes, seconds, direction);
    console.log(converDms2Decimal); // Output: 46.984722222222224

    // Converting Decimal to Degrees Minutes Sesonds
    const type = 'longitude';
    const decimal = -122.90222222222222;

    const converDecimal2DMS = Decimal2DMS(decimal, type);
    console.log(converDecimal2DMS) // Output: 122°54'7"W
Enter fullscreen mode Exit fullscreen mode

here are the link to the npm package and I am busy with Python.

https://www.npmjs.com/package/dms-to-decimal

Top comments (0)