Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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 CharSequence formatDegreesAsDecimalDegreesString(double fDegrees) {

        double[] degreesMinutes = decimalDegreesToDegreeMinutes(fDegrees);

        return String.format(Locale.US, (fDegrees < 0 ? "-" : "") + "%1$d %2$06.3f", (int) degreesMinutes[0],
                degreesMinutes[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 };
    }
}