我以这种格式得到纬度和长度
纬度23.1326799999999997, 经度72.200818333333333
但我想以这种格式
纬度 = 23.132680 且经度 72.200818
如何转换
double Latitude = 23.132679999999997;
int precision = Math.pow(10, 6);
double new_Latitude = double((int)(precision * Latitude))/precision;
这只会给你小数点后 6 位数字。
double d=23.132679999999997;
DecimalFormat dFormat = new DecimalFormat("#.######");
d= Double.valueOf(dFormat .format(d));
一旦我这样解决了我的问题——
String.format("%.6f", latitude);
返回值为字符串。因此,如果您需要字符串结果,您可以使用它。
如果您需要双倍,您可以使用Double.parseDouble()
方法进行转换。
所以你想把一个双精度四舍五入到任意位数,不是吗?
可以使用类似
DecimalFormat df = new DecimalFormat("#,###,##0.00");
System.out.println(df.format(364565.14343));
如果你有纬度和经度作为字符串,那么你可以做
latitude = latitude.substring(0,latitude.indexOf(".")+6);
当然,您应该通过检查字符串长度来检查"."后是否至少有6个字符