[英]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.