Java:检查两个双精度值是否与特定的小数位数匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2944344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Check if two double values match on specific no of decimal places
提问by tzippy
Comparing those two values shall result in a "true":
比较这两个值应得出“真”:
53.9173333333333 53.9173
采纳答案by kennytm
If you want a = 1.00001
and b = 0.99999
be identified as equal:
如果您希望a = 1.00001
并被b = 0.99999
认定为平等:
return Math.abs(a - b) < 1e-4;
Otherwise, if you want a = 1.00010
and b = 1.00019
be identified as equal, and both a
and b
are positive and not huge:
否则,如果你想a = 1.00010
和b = 1.00019
被确定为相等,都a
和b
为正,而不是巨大的:
return Math.floor(a * 10000) == Math.floor(b * 10000);
// compare by == is fine here because both sides are integral values.
// double can represent integral values below 2**53 exactly.
Otherwise, use the truncate
method as shown in Are there any functions for truncating a double in java?:
否则,请使用Javatruncate
中是否有截断双精度的函数中所示的方法?:
BigDecimal aa = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
aa = aa.setScale(4, BigDecimal.ROUND_DOWN);
bb = bb.setScale(4, BigDecimal.ROUND_DOWN);
return aa.equals(bb);
回答by Michael Borgwardt
Naively:
天真:
if(Math.abs(a-b) < 0.0001)
However, that's not going to work correctly for all values. It's actually impossible to get it to work as long as you're using double
, because double
is implemented as binary fractons and does not even havedecimal places.
但是,这不会适用于所有值。只要您使用double
,实际上就不可能让它工作,因为它double
是作为二进制分形实现的,甚至没有小数位。
You'll have to convert your values to String
or BigDecimal
to make meaningful tests about their decimal places.
您必须将您的值转换为String
或BigDecimal
对其小数位进行有意义的测试。
You may want to read the Floating-Point Guideto improve your understanding of how floating point values work.
您可能需要阅读浮点指南以提高您对浮点值工作原理的理解。
回答by tzippy
Thanks. I did it this way:
谢谢。我是这样做的:
double lon = 23.567889;
BigDecimal bdLon = new BigDecimal(lon);
bdLon = bdLon.setScale(4, BigDecimal.ROUND_HALF_UP);
System.out.println(bdLon.doubleValue());
回答by DaveEdelstein
Apache commons has this: org.apache.commons.math3.util.Precision equals(double x, double y, double eps)
Apache commons 有这个:org.apache.commons.math3.util.Precision equals(double x, double y, double eps)
epsilon would be the distance you would allow. Looks like yours would be 1e-5?
epsilon 将是您允许的距离。看起来你的将是 1e-5?
The source-code of this method looks like it uses Math.abs as suggested in other answers.
此方法的源代码看起来像其他答案中建议的那样使用 Math.abs。
回答by Norby
here is the simple example if you still need this :)
如果您仍然需要这个,这是一个简单的例子:)
public static boolean areEqualByThreeDecimalPlaces(double a, double b) {
a = a * 1000;
b = b * 1000;
int a1 = (int) a;
int b1 = (int) b;
if (a1 == b1) {
System.out.println("it works");
return true;
}
else
System.out.println("it doesn't work");
return false;