3

i get lat and long in this format

Latitude23.132679999999997, Longitude72.20081833333333

but i want to in this format

Latitude = 23.132680 and Longitude 72.200818

how can i convert

Sunny
  • 97
  • 2
  • 9

6 Answers6

7
double Latitude = 23.132679999999997;
int precision =  Math.pow(10, 6);
double new_Latitude = double((int)(precision * Latitude))/precision;

This will give you only 6 digits after decimal point.

Sergey Glotov
  • 19,479
  • 11
  • 78
  • 93
Shakti Malik
  • 2,303
  • 22
  • 32
5
double d=23.132679999999997;
DecimalFormat dFormat = new DecimalFormat("#.######"); 

d= Double.valueOf(dFormat .format(d));
MAC
  • 15,363
  • 8
  • 51
  • 92
2

Once I solved my problem like this -

String.format("%.6f", latitude);

Return value is string. So you can use this if you need string result.

If you need double you can convert using Double.parseDouble() method.

Shaiful
  • 5,433
  • 5
  • 36
  • 40
1

So you want round a double to an arbitrary number of digits, don't you?

Community
  • 1
  • 1
Eduardo
  • 3,932
  • 2
  • 42
  • 56
0

can use like

DecimalFormat df = new DecimalFormat("#,###,##0.00");
System.out.println(df.format(364565.14343));
Paresh Mayani
  • 122,920
  • 69
  • 234
  • 290
Dheeresh Singh
  • 15,446
  • 3
  • 35
  • 36
0

If you have Latitude and Longitude as String then you can do

latitude = latitude.substring(0,latitude.indexOf(".")+6);

Of course you should check that there are at least 6 characters after "." by checking string length

Uma Madhavi
  • 4,601
  • 5
  • 34
  • 70
Dhairya Vora
  • 1,283
  • 12
  • 33
  • Java variable names are start with lowercase letter & Classe names are start with uppercase letter. Try to maintain the conversion. At first glance I thought Latitude is the class name. – Shaiful May 15 '12 at 11:49