Java Double to doubleToSexagesimal(double value, int precision, double hopr, double lopr)

Here you can find the source of doubleToSexagesimal(double value, int precision, double hopr, double lopr)

Description

double To Sexagesimal

License

Open Source License

Declaration

private static String doubleToSexagesimal(double value, int precision, double hopr, double lopr) 

Method Source Code

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

public class Main {
    public static final int DEFAULT_PRECISION = 4;
    public static final int UNSET_PRECISION = -1;
    private static final int MAXPREC = 8;
    private static final double[] prec_tab = new double[] { 1.0, 1.0 / 6.0, 1.0 / 60.0, 1.0 / 360.0, 1.0 / 3.6E3,
            1.0 / 3.6E4, 1.0 / 3.6E5, 1.0 / 3.6E6, 1.0 / 3.6E7 };

    private static String doubleToSexagesimal(double value, int precision, double hopr, double lopr) {
        double prec_frac, range, hrs, frac;
        int i, min, sec;

        if (precision == UNSET_PRECISION) {
            precision = DEFAULT_PRECISION;
        }//from  ww w .j a  v a2  s .c  om

        /* Round the multiplier required to represent the value as an integer,
           retaining the required precision */
        if (precision <= MAXPREC) {
            prec_frac = prec_tab[precision];
        } else {
            prec_frac = prec_tab[MAXPREC];
            for (i = precision; i > MAXPREC; i--) {
                prec_frac *= 0.1;
            }
        }

        /* Add half the maximum displayed precision to aid with rounding */
        value = value + 0.5 * prec_frac;

        /* Normalise the value to within the range [hopr,lopr), if required */
        range = (hopr - lopr);
        if (range == 360.0 || range == 24.0) {
            value = value - Math.floor((value - lopr) / range) * range;
        }

        StringBuilder builder = new StringBuilder();

        /* Insert a leading negative sign, if required */
        if (value < 0.0) {
            builder.append('-');
            value = -value + prec_frac;
        }

        /* Now format the numbers */
        hrs = Math.floor(value);
        value = (value - hrs) * 60.0;
        min = (int) value;
        value = (value - min) * 60.0;
        sec = (int) value;

        if (precision == 0) {
            builder.append(String.format("%.0f", hrs));
        } else if (precision == 1) {
            builder.append(String.format("%.0f:%d", hrs, (min / 10)));
        } else if (precision == 2) {
            builder.append(String.format("%.0f:%02d", hrs, min));
        } else if (precision == 3) {
            builder.append(String.format("%.0f:%02d:%d", hrs, min, sec / 10));
        } else if (precision == 4) {
            builder.append(String.format("%.0f:%02d:%02d", hrs, min, sec));
        } else {
            frac = Math.floor((value - sec) / (prec_frac * 3600.0));
            builder.append(String.format("%.0f:%02d:%02d.%0" + (precision - 4) + ".0f", hrs, min, sec, frac));
        }

        return builder.toString();
    }
}

Related

  1. doubleToLex(double v)
  2. doubleToPercent(double value, int precision)
  3. doubleToRational(double number)
  4. doubleToRegisters(double d)
  5. doubleToScale(double val)
  6. doubleToSingleQuote(String source)
  7. doubleToSingleQuotes(String str)
  8. doubleToSlider(final double min, final double max, final double value)
  9. doubleToTime(double time)