diff --git a/src/decimalToSexagesimal.ts b/src/decimalToSexagesimal.ts index 0105e37..b96417c 100644 --- a/src/decimalToSexagesimal.ts +++ b/src/decimalToSexagesimal.ts @@ -5,7 +5,7 @@ const imprecise = (number: number) => { }; // Converts a decimal coordinate value to sexagesimal format -const decimal2sexagesimal = (decimal: number) => { +const decimal2sexagesimal = (decimal: number, locale?: string | string[]) => { const [pre, post] = decimal.toString().split('.'); const deg = Math.abs(Number(pre)); @@ -13,27 +13,14 @@ const decimal2sexagesimal = (decimal: number) => { const min = Math.floor(minFull); const sec = imprecise((minFull % min || 0) * 60); + const formatter = new Intl.NumberFormat(locale, {minimumIntegerDigits: 2}); + // We're limiting minutes and seconds to a maximum of 6/4 decimal places // here purely for aesthetical reasons. That results in an inaccuracy of // a few millimeters. If you're working for NASA that's possibly not // accurate enough for you. For all others that should be fine. // Feel free to create an issue on GitHub if not, please. - return ( - deg + - '° ' + - Number(min.toFixed(6)) - .toString() - .split('.') - .map((v, i) => (i === 0 ? v.padStart(2, '0') : v)) - .join('.') + - "' " + - Number(sec.toFixed(4)) - .toString() - .split('.') - .map((v, i) => (i === 0 ? v.padStart(2, '0') : v)) - .join('.') + - '"' - ); + return `${deg}° ${formatter.format(Number(min.toFixed(6)))}' ${formatter.format(Number(sec.toFixed(4)))}"`; }; export default decimal2sexagesimal;