2

I am currently try to retrieve a latitude and longitude value from a location. When i convert the location to integer values using the following code:

    LocationManager locMan;
    Location location;
    String towers;
    private static double lat;
    private static double lon;

    locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);          
    Criteria crit = new Criteria();

    towers = locMan.getBestProvider(crit, false);
    location = locMan.getLastKnownLocation(towers);                 

        if (location != null)
        {                   

            lat = (int) (location.getLatitude() * 1E6);
            lon = (int) (location.getLongitude() * 1E6);
            GeoPoint ourLocation = new GeoPoint(lati, longi);
            OverlayItem overlayItem = new OverlayItem(ourLocation, "1st String", "2nd String");
            CustomPinpoint custom = new CustomPinpoint(d, MainMap.this);
            custom.insertPinpoint(overlayItem);
            overlayList.add(custom);
            overlayList.clear();    

            lat = (double) lat;             
            lon = (double) lon; 

            System.out.println("Lat is  " + lat);
            System.out.println("Longi is  " + lon);
        }
        else
        {
            System.out.println("Location is null! " + towers);
            Toast.makeText(MainMap.this, "Couldn't get provider", Toast.LENGTH_SHORT).show();                   
        }

it comes back in the format of 0.000000

lat is 5.494394
long is -7.724457

how can i get it back in the format 00.000000

I have tried DecimalFormat, Math.Round and various other solutions i found on Stack Overflow but still get the same result. Please help!

devinefergal
  • 287
  • 1
  • 4
  • 10
  • Do you want to show it in the format 00.000000 in a string then? – Pedro Ferreira Mar 27 '12 at 12:35
  • no i need the value as an integer or double. The value of lat should be 54.94394 – devinefergal Mar 27 '12 at 12:39
  • Wait then its another issue. lat is 5.494394 but you want it to become 54.94394? Then just "lat *=10" – Pedro Ferreira Mar 27 '12 at 12:41
  • nope its not that simple, that just gives 5.494394E7 – devinefergal Mar 27 '12 at 13:27
  • 2
    There are a few flaws in what you posted. First: Integers don't have decimal values (or it rounds them if you prefer). double lat = (double) 5.494394; lat *=10; System.out.println(lat); >>Prints 54.94394 which is what you want. – Pedro Ferreira Mar 27 '12 at 13:34
  • I think you are not clear in your requirement. What is the type of lat? Can you post simple code like `double/String lat = [enter hardcoded value here]` and tell ud exactly what output you require (type & value)? – assylias Mar 27 '12 at 13:37
  • And why do you multiply by 1E6 ? – assylias Mar 27 '12 at 13:40
  • This question is *extremely* unclear. Latitude is a high-precision measure that requires a `double` value... I can't possibly think of a reason why you'd want to convert to `int`. Besides, if you need to format your `double` latitude with two zeros always, use one of the solutions available here and on other questions as well. They *do* work, and so your problem is elsewhere. Since you can't express your problem, unambiguously, with words, show us your code, which is (almost) always unambiguous. :) – davidcesarino Mar 27 '12 at 13:46
  • Apologies for the unclear question, I converted the values to int because the Geopoint method requires int values. – devinefergal Mar 27 '12 at 13:59
  • The solutions here convert the double to a string though? Im sorry im a novice programmer :) – devinefergal Mar 27 '12 at 14:10

4 Answers4

4

Have you tried this:

DecimalFormat sf = new DecimalFormat("00.000000");
String s = sf.format(5.494394);
System.out.println(s); //prints 05.494394

EDIT

Based on your new question, why don't you do this:

double latitude = location.getLatitude();
double longitude = location.getLongitude();
GeoPoint ourLocation = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
//....
System.out.println("Lat is  " + latitude);
System.out.println("Longi is  " + longitude);
assylias
  • 321,522
  • 82
  • 660
  • 783
  • but i need 54.94394 rather than 05.494394 how do i get that? – devinefergal Mar 27 '12 at 13:29
  • I require my latitiude value therefore 05.494394 is not relevant. When I remove a 0 from that method i get back 5.494394 The value I require is 54.94394 – devinefergal Mar 27 '12 at 13:33
  • Thanks so much @assylias Do you know how I can round the output of location.getLatitude(); My output at the minute is 54.95229833333333 – devinefergal Mar 27 '12 at 14:34
  • @devinefergal If you mean for printing, then you can use the first part of my answer and adjust the number of 0 after the decimal point. – assylias Mar 27 '12 at 14:46
2

Convert to String, add leading zero. StringFormatter might help. An integer will never have leading zeroes.

DKIT
  • 3,471
  • 2
  • 20
  • 24
  • I tried converting to a double like this: lat = (double) lat; But I need the value as an integer or double as i will use it to get distance between the location and another point. – devinefergal Mar 27 '12 at 12:36
1

You do a confusion between "real data" and "representation"

5.494394 is the "real data", that's an integer inferior to 10, it's logical to haven't a decade when you display it directly.

I you want to display every time the decade, also there is equal to 0, you have to test if your integer are inferior to 10 are not. With an atomic test, this can be done with this way in java:

(lat < 10) ? "0"+lat : lat;

with this function, you are always the decade displayed before the "real data".

Vincent Saluzzo
  • 1,377
  • 1
  • 11
  • 13
1
public String formatFigureToTwoPlaces(double value) {
    DecimalFormat myFormatter = new DecimalFormat("00.00");
    return myFormatter.format(value);
}
Ollie C
  • 28,313
  • 34
  • 134
  • 217