Java Degree Convert to degreesToString(final double value, final char positiveChar, final char negativeChar)

Here you can find the source of degreesToString(final double value, final char positiveChar, final char negativeChar)

Description

degrees To String

License

Open Source License

Declaration

public static String degreesToString(final double value, final char positiveChar, final char negativeChar) 

Method Source Code

//package com.java2s;
/*// w  w w  . j  a va2  s .c  om
 * Copyright 2007 Joachim Sauer
 * Copyright 2002-2006 Chriss Veness (vincenty formula in distance())
 * 
 * This file is part of bbTracker.
 * 
 * bbTracker is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * bbTracker is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static final char DEGREE = '\u00B0';
    public static final char MINUTE = '\'';
    public static final char SECOND = '"';

    public static String degreesToString(final double value, final char positiveChar, final char negativeChar) {
        if (Double.isNaN(value)) {
            return "-";
        }
        char c;
        double d;
        if (value < 0) {
            d = -value;
            c = negativeChar;
        } else {
            d = value;
            c = positiveChar;
        }
        final StringBuffer buf = new StringBuffer(13);
        buf.append(c);
        final int degrees = (int) Math.floor(d);
        d = (d - degrees) * 60;
        final int minutes = (int) Math.floor(d);
        d = (d - minutes) * 60;
        final int seconds = (int) Math.floor(d);
        d = (d - seconds) * 100;
        final int hundrethSeconds = (int) Math.floor(d + 0.5d);

        appendTwoDigits(buf, degrees, ' ').append(DEGREE);
        appendTwoDigits(buf, minutes, ' ').append(MINUTE);
        appendTwoDigits(buf, seconds, ' ').append('.');
        appendTwoDigits(buf, hundrethSeconds, '0').append(SECOND);
        return buf.toString();
    }

    private static StringBuffer appendTwoDigits(final StringBuffer buf, final int value, final char c) {
        if (value < 10) {
            buf.append(c);
        }
        buf.append(value);
        return buf;
    }
}

Related

  1. degreesToDecimal(String input)
  2. degreesToMetres(double degrees)
  3. degreesToPercent(float n)
  4. degreesToRA(double val)
  5. degreesToSexigessimal(double ra, double dec)
  6. toDecimalDegrees(String picaValue)
  7. toDegrees(double degrees, int nDecimals, boolean longitude)