Javascript 中的十进制度到度分和秒
我正在尝试编写一个函数,获取我的十进制度数(纬度或经度)并将它们转换为 DMS 度分秒。我知道我应该将小数点乘以 60,然后再次得到小数。但我是一个菜鸟。我会分割号码吗?
function ConvertDDToDMS(DD) {
eg. DD =-42.4
D= 42;
M= 4*60;
S= .M * 60;
var DMS =
return DMS //append Direction (N, S, E, W);
}
我走在正确的轨道上吗?
Im trying to write a function that takes my decimal degrees (lat or long) and converts them to DMS degrees minutes seconds. I know I am meant to times the decimal point number by 60 then it's decimal again. But am a noob. Would I split the number?
function ConvertDDToDMS(DD) {
eg. DD =-42.4
D= 42;
M= 4*60;
S= .M * 60;
var DMS =
return DMS //append Direction (N, S, E, W);
}
Am I on the right track?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
上面给出了一个对象
{deg, min, sec, dir}
,其中sec被截断为两位数(例如3.14
),dir是N
之一code>、E
、S
、W
取决于您是否将lng
(经度)参数设置为 true。例如:或者如果您只想要基本字符串:
[编辑 2019 年 6 月] - 修复一个 8 年前的错误,该错误有时会导致结果因转换精确分钟时的浮点数学而偏离 1 分钟,例如
转换DDToDMS(4 + 20/60)
。[编辑 2021 年 12 月]——哎呀。修复#2。返回原始代码,并将
1e-9
添加到 a) 将任何稍低的浮点错误提升到下一个最高数字的值中,并且 b) 小于.01
秒,因此对输出没有影响。将1e-4
添加到“字符串”版本,这是相同的修复,但也舍入秒(接近 1/2 秒)。The above gives you an object
{deg, min, sec, dir}
with sec truncated to two digits (e.g.3.14
) and dir being one ofN
,E
,S
,W
depending on whether you set thelng
(longitude) parameter to true. e.g.:Or if you just want the basic string:
[edit June 2019] -- fixing an 8 year old bug that would sometimes cause the result to be 1 minute off due to floating point math when converting an exact minute, e.g.
ConvertDDToDMS(4 + 20/60)
.[edit Dec 2021] -- Whoops. Fix #2. Went back to the original code and added
1e-9
to the value which a) bumps any slightly low floating point errors to the next highest number and b) is less than.01
sec, so has no effect on the output. Added1e-4
to the "string" version which is the same fix, but also rounds seconds (it's close to 1/2 sec).目前尚不清楚您如何需要输出。这是一个以字符串形式返回所有 3 个值的版本:
It's not clear how you need the output. Here's a version that returns all 3 values as a string:
更新:我删除了没有任何意义的部分(感谢cwolves!)。
这里还有另一个实现。它不会像以前那样简短或高效,但希望更容易理解。
为了做到正确,首先您需要了解计算是如何完成的,然后才尝试实现它们。为此,伪代码是一个很好的选择,因为您可以用简单的英语或易于理解的简化语法写下步骤,然后将其翻译成所选的编程语言。
我希望它有用!
Update: I remove the part that did not make any sense (thanks cwolves!).
Here you have yet another implementation. It won't be as short nor efficient as the previous ones, but hopefully much easier to understand.
To get it right, first you need to understand how the calculations are done and only then attempt to implement them. For that, pseudocode is a great option, since you write down the steps in plain English or a simplified syntax that is easy to understand, and then translate it onto the programming language of choice.
I hope it's useful!
这个在 TypeScript 中工作 %100:
This one works %100 in TypeScript:
尝试一下这个工作完美!
Try this working perfect!!!
一种解决方案,可以选择指定输出秒数的小数位,并纠正由于舍入秒和分钟而导致的任何边缘情况。
A solution with the option for specifying the decimal places in output seconds and correction of any edge cases due to rounding seconds and minutes.
输入:22.4229541515 输出:22:25:22.63
INPUT : 22.4229541515 OUTPUT: 22:25:22.63
我很惊讶所有解决方案都使用一些额外的逻辑来处理“舍入到 60”的情况(如果他们完全意识到这一点),但没有人想到以相反的方式做这件事,从(四舍五入的)秒开始,然后使用 mod 和 int-div 而不必担心所有这些:
抱歉,这没有 N/S、E/W 部分,需要一些额外的方法来调用它。
如果你想要第二个分数,你可以使用这个:
I'm surprised all solutions are using some additional logic to handle the "rounds to 60" cases (if they're aware of it at all), but nobody thought of doing it the other way round, starting with (rounded) seconds and then using mod and int-div and not have to worry about all that:
Sorry, this is without the N/S, E/W part, would need some additional method calling it.
If you want second-fractions, you could use this:
基于上面的答案,我已经将它们编写成 javascript 和 php 风格。
JS-
PHP-
Based on above answer, i've written them into javascript and php style.
JS-
PHP-
无法让上面的脚本工作,经过一段时间后想出了这个;
只需将 dms 提供给脚本
,反之亦然,只需将度数提供给脚本,以及 lat(纬度)的 true 或 false
希望这可以帮助人们。
couldnt get the script above working, after some time came up with this;
just give the dms to the script
and visa versa just give the degrees to the script, and true of false for lat (latitude)
hope this helps people..
这里的答案很好,但要以标准格式获取 DMS。
Good answers here but to get the DMS in the standard format.
仅供说明,答案
}
对于 -1° 和 0° 之间的角度不起作用。
厄运!
HC
just for remark, the answer
}
does not work for angles between -1° and 0°.
Bad luck!
hc