Java tutorial
//package com.java2s; /* ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. */ import java.util.Locale; public class Main { public static String formatDegreesCoordsAsStringNWSE(double degLat, double degLon) { double[] minutesLat = decimalDegreesToDegreeMinutes(degLat); double[] minutesLon = decimalDegreesToDegreeMinutes(degLon); return String.format(Locale.US, (degLat < 0 ? "S" : "N") + " %1$d %2$06.3f " + (degLon < 0 ? "W" : "E") + " %3$d %4$06.3f", (int) minutesLat[0], minutesLat[1], (int) minutesLon[0], minutesLon[1]); } public static double[] decimalDegreesToDegreeMinutes(double fDegrees) { final double fAbsDegrees = Math.abs(fDegrees); int dAbsDegrees = (int) fAbsDegrees; // Adjust degrees if minutes calulate to 60, due to rounding errors: double fAbsMinutes = 60.0 * (fAbsDegrees - dAbsDegrees); String minutesString = String.format(Locale.US, "%1$06.3f", fAbsMinutes); if (minutesString.equals("60.000")) { dAbsDegrees = dAbsDegrees + 1; fAbsMinutes = 0; } return new double[] { dAbsDegrees, fAbsMinutes }; } }