我以这种格式获得经纬度
纬度23.132679999999997,经度72.20081833333333
但我想以这种格式
纬度 = 23.132680 和经度 72.200818
我怎样才能转换
我以这种格式获得经纬度
纬度23.132679999999997,经度72.20081833333333
但我想以这种格式
纬度 = 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()
方法进行转换。
因此,您希望将 double 舍入为任意位数,不是吗?
可以使用
DecimalFormat df = new DecimalFormat("#,###,##0.00");
System.out.println(df.format(364565.14343));
如果您将纬度和经度作为字符串,那么您可以这样做
latitude = latitude.substring(0,latitude.indexOf(".")+6);
当然,您应该检查“。”之后是否至少有 6 个字符。通过检查字符串长度