Sunny
Sunny

Reputation: 97

remove latitude and longitude fraction part after 6 digit

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

Upvotes: 3

Views: 6847

Answers (6)

Dhairya Vora
Dhairya Vora

Reputation: 1281

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

Upvotes: 0

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

can use like

DecimalFormat df = new DecimalFormat("#,###,##0.00");
System.out.println(df.format(364565.14343));

Upvotes: 0

Shakti Malik
Shakti Malik

Reputation: 2407

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.

Upvotes: 7

Shaiful
Shaiful

Reputation: 5673

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.

Upvotes: 2

Eduardo
Eduardo

Reputation: 4382

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

Upvotes: 1

MAC
MAC

Reputation: 15847

double d=23.132679999999997;
DecimalFormat dFormat = new DecimalFormat("#.######"); 

d= Double.valueOf(dFormat .format(d));

Upvotes: 5

Related Questions