3

我以这种格式获得经纬度

纬度23.132679999999997,经度72.20081833333333

但我想以这种格式

纬度 = 23.132680 和经度 72.200818

我怎样才能转换

4

6 回答 6

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

这将只给您小数点后的 6 位数字。

于 2012-05-15T11:41:06.280 回答
6
double d=23.132679999999997;
DecimalFormat dFormat = new DecimalFormat("#.######"); 

d= Double.valueOf(dFormat .format(d));
于 2012-05-15T11:37:53.750 回答
2

一旦我像这样解决了我的问题-

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

返回值为字符串。因此,如果您需要字符串结果,您可以使用它。

如果您需要双精度,您可以使用Double.parseDouble()方法进行转换。

于 2012-05-15T12:04:30.590 回答
1

因此,您希望将 double 舍入为任意位数,不是吗?

于 2012-05-15T11:38:00.973 回答
0

可以使用

DecimalFormat df = new DecimalFormat("#,###,##0.00");
System.out.println(df.format(364565.14343));
于 2012-05-15T11:37:58.700 回答
0

如果您将纬度和经度作为字符串,那么您可以这样做

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

当然,您应该检查“。”之后是否至少有 6 个字符。通过检查字符串长度

于 2012-05-15T11:39:59.540 回答