Java Fraction Format formatDegToDms(double degrees, double min, double max)

Here you can find the source of formatDegToDms(double degrees, double min, double max)

Description

format Deg To Dms

License

Open Source License

Declaration

public static String formatDegToDms(double degrees, double min, double max) 

Method Source Code


//package com.java2s;
/*/*from   ww w  . ja  va2  s .  c o m*/
 * Copyright (C) 2011-2012 Inaki Ortiz de Landaluce Saiz
 * 
 * This program is free software: you can redistribute it 
 * and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either 
 * version 3 of the License, or (at your option) any later version.
 *
 * This program 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 Lesser General Public 
 * License along with this program. If not, see 
 * <http://www.gnu.org/licenses/>
 */

import java.text.DecimalFormat;

public class Main {
    public static String formatDegToDms(double degrees, double min, double max) {
        String s = "";
        // normalize values within the given range
        double deg = normalizeAngle(degrees, min, max);
        // take sign into account
        if (deg < 0) {
            deg = Math.abs(deg);
            s += "-";
        }
        double arcmin = (deg - (int) deg) * 60.0;
        double arcsec = (arcmin - (int) arcmin) * 60.0;
        s += new DecimalFormat("00").format((int) deg) + "d" + new DecimalFormat("00").format((int) arcmin) + "'"
                + new DecimalFormat("00.00").format(arcsec) + "\"";
        return s;
    }

    /**
     * Normalizes an angle value between a given range
     * 
     * @param value
     *          the angle value
     * @param minValue
     *          minimum value of the range
     * @param maxValue
     *          maximum value of the range
     */
    public static double normalizeAngle(double value, double minValue, double maxValue) {
        double delta = maxValue - minValue;
        return ((value - minValue) % delta + delta) % delta + minValue;
    }
}

Related

  1. formatDecimal(double value, String mask)
  2. formatDecimal(String format, double value)
  3. formatDecimals(double d, int mantissa)
  4. formatDecimals(double number)
  5. formatDecNumber(Double number, String pattern)
  6. formatDisplay(Double value)
  7. formatDistance(float distance)
  8. formatDollarAmount(float amount)
  9. formatDoube(double val)