DEV Community

Cover image for Converting DMS (Degrees, Minutes, Seconds) to Decimal and Vice Versa
Reagan Scofield
Reagan Scofield

Posted on • Edited on

Converting DMS (Degrees, Minutes, Seconds) to Decimal and Vice Versa

Whenever I search for an NPM package that meets my requirements and come up empty-handed, I take it upon myself to write and publish a solution. Last week, I was working on a feature that required converting Degrees, Minutes, and Seconds (DMS) to Decimal format and vice versa. Since I couldn't find a suitable existing tool, I developed my own package to handle both cases.

The Dms2Decimal function accepts four parameters: degrees, minutes, and seconds as integers, and direction as a string. It returns 0 if the input is invalid and the calculated decimal value if successful.

The Decimal2Dms function takes two parameters: a decimal float and a type string (either "latitude" or "longitude"). It returns null if not found or a formatted string (e.g., 23°34'5"N) upon success.

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)