x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 421

Format value of decimel to latitude/longitude

Hi I need to format a decimel value example    -34.1605774  to a latitude/longitude format of    DDMMSS S Please Help
0
mixmates
• 4
• 4
• 2
• +1
1 Solution

Commented:
What do You mean by that  DDMMSS S?
0

Author Commented:
DD = Decimel Degrees    MM = Minutes   SS = Seconds S = South the minus at the beginning of that double would represent South a plus would represent North Depending on lat long a plus can be North or East a minus South or West
0

Commented:
can u eplain the question bit more?
0

Author Commented:
I'll try:

in a text field I enter a set of coordinates 470255N 1253755W. These are taken as a string and turned into a double 47.0255 and -125.3755 I need to be able to take it from one format to the other. The coordinates being a string turned into a double and a double into a string with a positive denoting a North minus denoting a South on the first set of numbers and positive denoting East on the second set with a negative denoting west. Mostly I need help going from string to double and back
0

Commented:
Is this homework?
0

Author Commented:
No I am new to Java admittedly but I are no longer student I am greduated ; ) I am switching to programming after years of being a sysadmin
0

Commented:
try this,

String val = textField.getText() ;

char c = val.charAt( va.length() - 1 ) ;

int divider = 10000 ;
double dval = Double.parseDouble( val.subString( 0, va.length() - 2 ) ) ;
dval = c == 'N' ? ( dval / devider ) : ( -1 * dval/devider ) ;

pls correct if any syntax err
0

Commented:
This may be simplified but you could do

static class Sexagesimal {
private int seconds;
private int minutes;
private int degrees;

public void setSeconds(int seconds) {
this.seconds = seconds;
}

public int getSeconds() {
return seconds;
}

public void setMinutes(int minutes) {
this.minutes = minutes;
}

public int getMinutes() {
return minutes;
}

public void setDegrees(int degrees) {
this.degrees = degrees;
}

public int getDegrees() {
return degrees;
}

public String toString() {
return new StringBuffer()
.append(degrees)
.append("\u00B0")
.append(" ")
.append(minutes)
.append("'")
.append(" ")
.append(seconds)
.append("''")
.toString();
}

}

public static Sexagesimal decimalDegreesToSexagesimal(double degrees) {
Sexagesimal result = new Sexagesimal();
String whole = Double.toString(degrees);
String d = whole.substring(0, whole.indexOf("."));
String dec = whole.substring(whole.indexOf("."));
result.setDegrees(Integer.parseInt(d));
double D = Double.parseDouble(dec) * 60.0;
whole = Double.toString(D);
d = whole.substring(0, whole.indexOf("."));
result.setMinutes(Integer.parseInt(d));
dec = whole.substring(whole.indexOf("."));
result.setSeconds((int)(Double.parseDouble(dec) * 60.0));
return result;
}
0

Commented:
-34° 9' 38''

is the result given for your example
0

Author Commented:
Thanks so much for everyones help
0

Commented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.