简体   繁体   English

为什么我的android应用程序中的计算结果错误?

[英]Why do I get a wrong result for this calculation in my android app?

In my app i am using this to calculate a number: 在我的应用程序中,我使用它来计算数字:

double result = ((a * a) + (b * b) - (2 * a * b));

If a for example a = 2 and b = 3.9, then the result should be 3.61. 例如,如果a = 2且b = 3.9,则结果应为3.61。 However, when I display "result" i get the number 3.610000000000001. 但是,当我显示“结果”时,我得到的数字是3.610000000000001。

What is wrong here? 怎么了 How can I correct this error? 我该如何纠正该错误?

You can format your output value by using the following construct: 您可以使用以下结构来格式化输出值:

String result;
twoDForm = new DecimalFormat("#0.00");
result = twoDForm.format(value);

After this, in result you will have your value in X.XX format 之后,您将获得X.XX格式的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的Android应用程序中的GPS距离计算错误? - Why am I getting the wrong GPS distance calculation in my Android app? 为什么在安装我的应用程序时输入错误的快捷方式名称? - Why do I get the wrong shortcut name during installation of my app? 当我的Android应用程序在我的服务器上调用我的API时,为什么会得到空响应? - Why do I get an empty response when my android app calls my API on my server? 当Facebook与我的Android App集成时,为什么会出现App not Setup错误? - Why do I get App not Setup error when Facebook is integrated with my Android App? 为什么我在android ndk应用程序中得到错误的argc值? - why i get wrong argc value in android ndk app? 为什么我不能从startActivityForResult取回结果? - Why do I not get the result back from my startActivityForResult? 为什么我的Android应用程序出现“不幸的是myapp已停止”的问题? - Why do I get “unfortunately myapp has stopped” in my Android app? 如何在Android App中获得CPU调速器? - How do I get my CPU Governor in an Android App? 如何获取我的 android 应用程序的本地货币代码? - How do I get the local Currency Code for my android app? 为什么我在我的 Android 应用程序中使用的可绘制对象在用作按钮背景时纵横比错误 - Why drawables I use in my Android app have wrong aspect ratio when used as buttons background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM