Java Decimal From toDecimal(String coord)

Here you can find the source of toDecimal(String coord)

Description

Converts degree minute second gps coordinate to decimal coordinate form

License

Open Source License

Parameter

Parameter Description
coord the String gps coordinate to be converted

Return

the converted coordinate as a float

Declaration

public static float toDecimal(String coord) 

Method Source Code

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

public class Main {
    /**//  w  ww  . j a va2 s. co  m
     * Converts degree minute second gps coordinate to decimal coordinate form
     * 
     * @param coord
     *            the String gps coordinate to be converted
     * @return the converted coordinate as a float
     */
    public static float toDecimal(String coord) {
        // The indices for the two decimal points in the String coord
        int firstDec = coord.indexOf(".");
        int lastDec = coord.lastIndexOf(".");

        float deg = Float.parseFloat(coord.substring(0, firstDec));
        float min = Float.parseFloat(coord.substring(firstDec + 1, lastDec));
        float sec = Float.parseFloat(coord.substring(lastDec + 1, coord.length()));

        if (deg > 0) {
            return (deg + (min / 60 + sec / 3600));
        } else {
            return (deg - (min / 60 + sec / 3600));
        }
    }
}

Related

  1. toDecimal(final String intValue)
  2. toDecimal(float value)
  3. toDecimal(int value, byte[] buffer, int offset, int length, int itemLength, boolean packed)
  4. toDecimal(long val, int places)
  5. toDecimal(String number)