Java转Double类型经纬度为度分秒格式
时间: 2023-10-23 10:10:58 浏览: 49
可以使用以下代码将Java中的经纬度转换为度分秒格式:
```java
public static String convertToDMS(double coordinate) {
int degrees = (int) coordinate;
int minutes = (int) ((coordinate - degrees) * 60);
double seconds = (coordinate - degrees - minutes / 60.0) * 3600;
return degrees + "°" + minutes + "'" + String.format("%.2f", seconds) + "\"";
}
```
使用示例:
```java
double longitude = 116.4074;
double latitude = 39.9042;
String dmsLongitude = convertToDMS(longitude); // 116°24'26.64"
String dmsLatitude = convertToDMS(latitude); // 39°54'15.12"
```
其中,`convertToDMS`方法将输入的经度或纬度转换为度分秒格式的字符串。首先将度数部分取整作为度数,然后将小数部分乘以60取整作为分钟数,最后将剩余的小数部分乘以3600得到秒数。最后将度、分、秒按照格式拼接成字符串返回。
相关问题
QT转QString类型经纬度为度分格式
可以使用以下代码将经纬度转换为度分格式:
```cpp
QString degreeToDMS(double coordinate)
{
int degrees = static_cast<int>(coordinate);
double minutes = (coordinate - degrees) * 60;
QString direction = coordinate > 0 ? "E" : "W"; // 或者 "N" / "S",根据需要修改
QString dms = QString::fromUtf8("%1°%2'%3\" %4")
.arg(degrees)
.arg(static_cast<int>(minutes))
.arg(QString::number(minutes - static_cast<int>(minutes), 'f', 2).mid(2))
.arg(direction);
return dms;
}
```
使用方法:
```cpp
double longitude = 116.397128;
double latitude = 39.916527;
QString longitudeDMS = degreeToDMS(longitude);
QString latitudeDMS = degreeToDMS(latitude);
```
输出结果:
```
116°23'49.66" E
39°54'59.50" N
```
注意:此代码仅适用于正数经纬度。如果需要处理负数经纬度,需要修改代码中的判断和输出。
经纬度转度分秒java
以下是一个简单的 Java 代码示例,可以将经纬度从十进制度转换为度分秒:
```java
public static String decimalToDMS(double decimal) {
int degrees = (int) decimal;
decimal -= degrees;
decimal *= 60;
int minutes = (int) decimal;
decimal -= minutes;
decimal *= 60;
double seconds = Math.round(decimal * 100) / 100.0;
return degrees + "°" + minutes + "'" + seconds + "\"";
}
```
您可以将此方法用于经度和纬度,分别调用两次。例如:
```java
double longitude = 123.456789; // 经度
double latitude = 42.987654; // 纬度
String dmsLongitude = decimalToDMS(longitude);
String dmsLatitude = decimalToDMS(latitude);
System.out.println("经度: " + dmsLongitude);
System.out.println("纬度: " + dmsLatitude);
```
输出结果应该是类似这样的:
```
经度: 123°27'24.44"
纬度: 42°59'15.55"
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)