double a = 99.0123456789123456789d;
double b = 99.0123456789123456788d;String.valueOf(a) :99.01234567891234
14位后面的都被截取掉了,怎么判断a 都等于b怎样才能让精确度更高能,并判断a 是否等于b ?
double b = 99.0123456789123456788d;String.valueOf(a) :99.01234567891234
14位后面的都被截取掉了,怎么判断a 都等于b怎样才能让精确度更高能,并判断a 是否等于b ?
double a = 99.0123456789123456789d;
BigDecimal aa = new BigDecimal(a);
这样比较还是相等啊?
double a = 99.0123456789123456789d;
double b = 99.0123456789123456788d;
if(a == b){
System.out.println("a = b");
}
else {
System.out.println("a != b");
}
}
直接比。
因为你使用String来表示这个double的时候就会有精度损失,但是其大小是不变的
精度只能到99.01234567891234。而BigDecimal a = new BigDecimal(99.0123456789123456789d);
99.012345678912339508315199054777622222900390625
的精度实质也到99.01234567891234。所以:
public static void main(String argv[]){
BigDecimal a = new BigDecimal(99.0123456789123456789d);
BigDecimal b = new BigDecimal(99.0123456789123456788d);
if(String.valueOf(a).equals(String.valueOf(b))){
System.out.println("a = b");
}
else {
System.out.println("a != b");
}
System.out.println(String.valueOf(a)+"\n"+String.valueOf(b));
}
结果一样。
long double b = 99.0123456789123456788d;
在比较应该能行吧
public static void main(String argv[]){
BigDecimal a = new BigDecimal("99.0123456789123456789");
BigDecimal b = new BigDecimal("99.0123456789123456788");
if(String.valueOf(a).equals(String.valueOf(b))){
System.out.println("a = b");
}
else {
System.out.println("a != b");
}
System.out.println(String.valueOf(a)+"\n"+String.valueOf(b));
}
上几例中问题都出在99.0123456789123456789d 的到的是double型,这时就被截成了99.01234567891234。
double a = 99.0123456789123456789d;
double b = 99.0123456789123456788d;
的比较问题,怎样才能不损失精度,转换成BigDecimal
public static void main(String argv[]){
BigDecimal a = new BigDecimal("99.0123456789123456789");
BigDecimal b = new BigDecimal("99.0123456789123456788");
if(String.valueOf(a).equals(String.valueOf(b))){
System.out.println("a = b");
}
else {
System.out.println("a != b");
}
System.out.println(String.valueOf(a)+"\n"+String.valueOf(b));
}output:a != b
99.0123456789123456789
99.0123456789123456788
BigDecimal a2 = new BigDecimal("99.0123456789123456789");
BigDecimal b2 = new BigDecimal("99.0123456789123456788");
if (a2.compareTo(b2) == 0) {
System.out.println("equals");
}
99012345678912339508315199054777622222900390625以上是你的两个双精度数在BigDecimal 类中的值,可以看到他们两个是一样的,没法比较
BigDecimal bB = new BigDecimal(99.0123456789123456788d);
System.out.println("bA=" + bA);
System.out.println("bB=" + bB);
输出如下:
bA=99.012345678912339508315199054777622222900390625
bB=99.012345678912339508315199054777622222900390625
其实在99.0123456789123456789d转化成bA之前,99.0123456789123456789d先转成了99.01234567891234,所以其实执行的是BigDecimal bA = new BigDecimal(99.01234567891234);
BigDecimal bB = new BigDecimal(99.01234567891234);
当然bA还是与bB相等附:JAVADOC
public BigDecimal(double val)Translates a double into a BigDecimal. The scale of the BigDecimal is the smallest value such that (10scale * val) is an integer.
Note: the results of this constructor can be somewhat unpredictable. One might assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal to .1000000000000000055511151231257827021181583404541015625. This is so because .1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to .1, appearances notwithstanding. The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal(".1") is exactly equal to .1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.
BigDecimal bA = new BigDecimal("99.0123456789123456789");
BigDecimal bB = new BigDecimal("99.0123456789123456788");
System.out.println("bA=" + bA);
System.out.println("bB=" + bB);
int i = bA.compareTo(bB);
System.out.println(i);
输出如下:
bA=99.0123456789123456789
bB=99.0123456789123456788
1