Skip to content

Instantly share code, notes, and snippets.

@fajarlabs
Created November 22, 2016 05:56
Show Gist options
  • Save fajarlabs/0ecd153ce3a46d40e380a02569414bfb to your computer and use it in GitHub Desktop.
Save fajarlabs/0ecd153ce3a46d40e380a02569414bfb to your computer and use it in GitHub Desktop.
Convert Latitude or Longitude to DMS
import java.text.DecimalFormat;
public class DMSConverter {
public static DecimalFormat df = new DecimalFormat("#");
public static String lat(double vLat) {
double convertLat = Math.abs(vLat);
double dl = Math.floor(convertLat);
double minFloatL = (convertLat - dl) * 60;
double ml = Math.floor(minFloatL);
double secFloatL = (minFloatL-ml) * 60;
double sl = secFloatL;
String LatCardinal = ((vLat > 0) ? "N" : "S");
return df.format(dl)+"° "+df.format(ml)+"' "+df.format(sl)+"\""+" "+LatCardinal;
}
public static String lon(double vLng) {
double convertLng = Math.abs(vLng);
double dg = Math.floor(convertLng);
double minFloatG = (convertLng - dg) * 60;
double mg = Math.floor(minFloatG);
double secFloatG = (minFloatG-mg) * 60;
double sg = secFloatG;
String LngCardinal = ((vLng > 0) ? "E" : "W");
return df.format(dg)+"° "+df.format(mg)+"' "+df.format(sg)+"\""+" "+LngCardinal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment