我用double(小数)得到了这个问题.
当double = 1.234567然后我用,String.format("%.3f", myString);
所以结果是1.234
但当我的双倍是10
结果将是10,000
我希望这是10
他们是否可以说他只需要在"有用"时显示小数?
我看到一些关于这个的帖子,但那是php或c#,找不到关于android/java的东西(也许我看起来不太好).
希望你们能帮我解决这个问题.
编辑,现在我使用这样的东西:myString.replace(",000", "");
但我认为他们是一个更"友好"的代码.
在我的Android应用程序中,我试图计算两个位置之间的距离,但获得的值却是数以千万计的1100万以上。两点/位置之间的实际距离仅为1.1km-1.3Km。为什么会这样呢?即使.distanceTo方法返回的值以米为单位,1100万米仍然是一个很大的值。
这是我的代码:
Location locationA = new Location("LocationA");
locationA.setLatitude(lat);
locationA.setLongitude(lang);
Location locationB = new Location("LocationB");
locationB.setLatitude(14.575224);
locationB.setLongitude(121.042475);
float distance = locationA.distanceTo(locationB);
BigDecimal _bdDistance;
_bdDistance = round(distance,2);
String _strDistance = _bdDistance.toString();
Toast.makeText(this, "distance between two locations = "+_strDistance, Toast.LENGTH_SHORT).show();
public static BigDecimal round(float d, int decimalPlace) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd;
}
Run Code Online (Sandbox Code Playgroud)