Java Degree Convert to degreesToSexigessimal(double ra, double dec)

Here you can find the source of degreesToSexigessimal(double ra, double dec)

Description

degrees To Sexigessimal

License

Open Source License

Declaration

public static String[] degreesToSexigessimal(double ra, double dec) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.NumberFormat;

public class Main {
    private static NumberFormat raFormat = NumberFormat.getInstance();
    private static NumberFormat decFormat = NumberFormat.getInstance();

    public static String[] degreesToSexigessimal(double ra, double dec) {
        return new String[] { degreesToRA(ra), degreesToDEC(dec) };
    }//from  w ww .  j a va  2  s  .  com

    public static String degreesToRA(double val) {
        // raneg reduction to [0.0,360.0)
        while (val < 0.0) {
            val += 360.0;
        }
        while (val >= 360.0) {
            val -= 360.0;
        }

        // 24 hours/360 degrees = 15 deg/hour
        int h = (int) (val / 15.0);
        val -= h * 15.0;
        // 15 deg/hour == 0.25 deg/min == 4 min/deg
        int m = (int) (val * 4.0);
        val -= m / 4.0;
        // 4 min/deg == 240 sec/deg
        val *= 240.0;

        String hh = Integer.toString(h);
        String mm = Integer.toString(m);

        if (h < 10) {
            hh = "0" + h;
        }
        if (m < 10) {
            mm = "0" + m;
        }

        return (hh + ":" + mm + ":") + raFormat.format(val);
    }

    public static String degreesToDEC(double val) {
        if (val < -90.0 || val > 90.0) {
            throw new IllegalArgumentException("value " + val + " out of bounds: [-90.0, 90.0]");
        }
        String sign = "+";
        if (val < 0.0) {
            sign = "-";
            val *= -1.0;
        }
        int deg = (int) (val);
        val -= deg;
        // 60 min/deg
        int m = (int) (val * 60.0);
        val -= m / 60.0;
        // 60 sec/min == 3600 sec/deg
        val *= 3600.0;
        String d = Double.toString(val);

        String degs = Integer.toString(deg);
        if (deg < 10) {
            degs = "0" + degs;
        }
        String min = Integer.toString(m);
        if (m < 10) {
            min = "0" + m;
        }

        String s = sign + degs + ":" + min + ":";

        return s + decFormat.format(val);
    }
}

Related

  1. degreesMinSecToDegrees(String position)
  2. degreesToDecimal(String input)
  3. degreesToMetres(double degrees)
  4. degreesToPercent(float n)
  5. degreesToRA(double val)
  6. degreesToString(final double value, final char positiveChar, final char negativeChar)
  7. toDecimalDegrees(String picaValue)
  8. toDegrees(double degrees, int nDecimals, boolean longitude)