有没有办法可以减少小数点后的位数?

时间:2014-01-17 07:58:22

标签: gps coordinates

我的Android应用程序有点麻烦。我想使用手机的GPS显示当前的纬度和经度。我能够获得手机的当前坐标,但它显示为双倍值...我认为小数点后6 - 8位数。有没有办法可以减少小数点后的位数?

这是我的代码:

public class Localisation extends Activity implements LocationListener{

protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
private String TripDescription;
private String tripID;



TextView txtLat;
String lat;
String provider;
protected String latitude, longitude;

protected boolean gps_enabled, network_enabled;


@Override

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_localisation);
    txtLat = (TextView) findViewById(R.id.textView1);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

}


@Override
public void onLocationChanged(Location location) {
    txtLat = (TextView) findViewById(R.id.textView1);
    txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());

}

@Override
public void onProviderDisabled(String arg0) {
    Log.d("Latitude","disable");

}

@Override
public void onProviderEnabled(String provider) {
    Log.d("Latitude","enable");     
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("Latitude","status");     
}

1 个答案:

答案 0 :(得分:1)

将(浮动或双倍)减少到6位数:

double oldVal = location.getLatitude();
double newVal = ((int) Math.round(oldVal * 1E6)) / 1E6;

乘以一百万个截断6位数之后的所有内容。 在重新划分后,该值限制为6位数。