如何四舍五入经度和纬度值

How to round off longitude and latitude values

如何将这些经度和纬度值四舍五入到小数点后 6 位?

p1 包含经度和纬度值,"latValue" 和 "longValue" 分别包含它们各自的值。

p1 = new LatLng((float) (location.getLatitude()),
                    (float) (location.getLongitude()));

float latValue = (float) p1.latitude;
float longValue = (float) p1.longitude;

使用小数格式

示例:

float myFloat = 3.123123123f;
DecimalFormat df = new DecimalFormat("#.######");
df.setRoundingMode(RoundingMode.CEILING);

System.out.println(df.format(myFloat));

你的情况

System.out.println(df.format(latValue));
System.out.println(df.format(lonValue));

由于浮点值(即 float 和 double)表示为二进制小数而不是小数,因此实际上没有任何方法可以将值四舍五入到存储值中的任何给定精度。

您可以在使用 String.format("%.6f", p1.latitude)

打印值时执行此操作

可以说,您可以使用类似下面的内容,但它会立即变回浮点数,因此再次变得不精确:

round(latValue * 1000000.0) / 1000000.0