如何解决为什么浮点运算会在C#PointF中返回整数?
我有以下语句使用所有平面数据类型。
ScaledCordinates.BottomRight = new PointF(((float)ScaleBottomRight.X * (float)Scale),((float)ScaleBottomRight.Y * (float)Scale));
分配给"ScaleBottomRight.X" is 2.36523485
和"ScaleBottomRight.Y" is 2.38020468
的值。
"Scale" = 66.80098
。
我不明白为什么"ScaledCordinates.BottomRight"
在我的应用中给我{X = 158 Y = 159}
。
为什么float操作返回整数值?有时甚至令人沮丧的是输出是整数,有时是浮点数。我在double和float之间进行了各种转换,但结果相同。解决这些问题的最佳方法是什么?输出应为“浮动”数据类型。
解决方法
public void sendGetRequest(String url){
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(30,TimeUnit.SECONDS);
builder.readTimeout(30,TimeUnit.SECONDS);
builder.writeTimeout(30,TimeUnit.SECONDS);
httpClient = builder.build();
Request request = new Request.Builder().url(url).build();
try (Response response = httpClient.newCall(request).execute()) {
String responseBody = response.body().string();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String perfectJSON = gson.toJson(JsonParser.parseString(responseBody));
JSONObject json = new JSONObject(perfectJSON);
JSONObject business = json.getJSONObject("business_profile");
String phone = business.getString("support_phone");
String id = json.getString("id");
this.displayAlert(id+" > "+phone,perfectJSON);
}catch (Exception e){
e.printStackTrace();
}
}
和
2.36523485 * 66.80098 = 158.000005910153
它们的小数部分都太小而无法满足浮点精度,根据the docs约为6-9位数。尽管相乘的结果仍然是2.38020468 * 66.80098 = 159.0000052245864
(即float
):
System.Single
,
我假定无论类型ScaledCordinates.BottomRight
是什么,(字段的)X
和Y
属性都是类型int
。
例如,它可能是类型Point,其X和Y是int
s:X docs。 Y docs。
您可以将其更改为其他类型。但是只要它们是int
,就无法保留浮点信息。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。