We are doing the following exercise: Sexagesimal degree. We would like to convert a decimal input, for example
lat: 48.858222
lon: 2.2945
to a sexagesimal result as:
48° 51′ 30″ N, 2° 17′ 40″ E
We have written the following code:
import java.util.*;
public class SexagesimalDegree {
/**
* Convert WGS84-Coordinates from decimal degrees in sexagesimal degrees
*
* @param lat Latitute as Double
* @param lon Longtitute as Double
* @return String "%d° %d′ %d″ %s, %d° %d′ %d″ %s" (lat_degree, lat_minute, lat_second, lat_direction,
* lon_degree, lon_minute, lon_second, lon_direction)
* @throws Exception
*/
public static String convert /**/ (double lat, double lon) throws Exception {
System.out.println("lat: "+lat);
System.out.println("lon: "+lon);
String[] latSplitted = String.valueOf(lat).split("\\.");
String[] lonSplitted = String.valueOf(lon).split("\\.");
System.out.println("latSplitted: "+Arrays.toString(latSplitted));
System.out.println("lonSplitted: "+Arrays.toString(lonSplitted));
String latHours = latSplitted[0];
String lonHours = lonSplitted[0];
System.out.println("latHours: "+latHours);
System.out.println("lonHours: "+lonHours);
double latMinutesNumber = Double.parseDouble(latSplitted[1])*60;
double lonMinutesNumber = Double.parseDouble(lonSplitted[1])*60;
System.out.println("latMinutesNumber: "+latMinutesNumber);
System.out.println("lonMinutesNumber: "+lonMinutesNumber);
String latMinutes = String.valueOf((int)(latMinutesNumber)).substring(0,2);
String lonMinutes = String.valueOf((int)(lonMinutesNumber)).substring(0,2);
System.out.println("latMinutes: "+latMinutes);
System.out.println("lonMinutes: "+lonMinutes);
String latDecimalPart = "0"+String.valueOf(latMinutesNumber).substring(String.valueOf(latMinutesNumber).indexOf("."));
String lonDecimalPart = "0"+String.valueOf(lonMinutesNumber).substring(String.valueOf(lonMinutesNumber).indexOf("."));
System.out.println("latDecimalPart: "+latDecimalPart);
System.out.println("lonDecimalPart: "+lonDecimalPart);
System.out.println("Double.parseDouble(latDecimalPart): "+Double.parseDouble(latDecimalPart));
double remainingLatSeconds = Integer.parseInt(latMinutes)-Double.parseDouble(latDecimalPart);
System.out.println("remainingLatSeconds: "+remainingLatSeconds);
return "";
}
}
Which outputs the following results:
lat: 48.858222
lon: 2.2945
latSplitted: [48, 858222]
lonSplitted: [2, 2945]
latHours: 48
lonHours: 2
latMinutesNumber: 5.149332E7
lonMinutesNumber: 176700.0
latMinutes: 51
lonMinutes: 17
latDecimalPart: 0.149332E7
lonDecimalPart: 0.0
Double.parseDouble(latDecimalPart): 1493320.0
remainingLatSeconds: -1493269.0
As you would notice we are able to get hours and minutes as expected. However the puzzle part comes when we struggle to get the seconds. We would need to get from latitude the decimal part: 0.1493327E7, and from minutes its decimal part: 0.6700.
However we are getting 0.149332E7 for the latitude and 0.0 for the longitude. The doubt is: how could we get the remaining decimal, and calculate the seconds?
To be able to debug it we have read:
https://www.thoughtco.com/decimal-degrees-conversion-1434592
https://stackoverflow.com/questions/36474338/converting-string-to-double-with-dotç
https://stackoverflow.com/questions/5769669/convert-string-to-double-in-java
https://stackoverflow.com/questions/27480633/converting-from-string-to-double?noredirect=1&lq=1
To sum up, how could we calculate the remaining decimals, after calculating the minutes, to be able to get the seconds?