测试 BigDecimal 是否可以改变其他 BigDecimal 的整数部分
Test if BigDecimal can change integral part of other BigDecimal
我需要一种方法将其表达为有效的 java 代码:我有 2 个 BigDecimals
,我想知道较小的 BigDecimal
是否可以(添加到较大的BigDecimal
一次) 改变较大BigDecimal
的整数部分。示例:
0.6; 0.4 ->true
0.6; 0.39 ->false
有没有有效的方法,或者我必须测试?
在意识到 Titus 的回答更简单后,我编辑了我的回答。归功于 him/her。
public static boolean integral(BigDecimal a, BigDecimal b) {
BigDecimal greater;
if (a.compareTo(b) == 1) {
greater = a;
} else {
greater = b;
}
BigDecimal sum = a.add(b);
return (sum.intValue() != greater.intValue());
}
你可以这样做:
BigDecimal b1 = new BigDecimal("0.90");
BigDecimal b2 = new BigDecimal("0.20");
if(b1.add(b2).intValue() > Math.max(b1.intValue(), b2.intValue())){
System.out.println("The integral has changed");
}else {
System.out.println("The integral is the same");
}
intValue()
方法的工作原理:
Converts this BigDecimal to an int. This conversion is analogous to the narrowing primitive conversion from double to short ...
Specification: any fractional part of this BigDecimal will be discarded ...
private static boolean isNonFractionalChanged(BigDecimal a, BigDecimal b) {
BigInteger aInt = a.toBigInteger();
BigInteger aNewInt = a.add(b).toBigInteger();
return aInt.compareTo(aNewInt) != 0;
}
以上即使对于非常大的数字也适用。
BigDecimal a = new BigDecimal("0.90");
BigDecimal b = new BigDecimal("0.20");
System.out.println(a + " + " + b + " = " + a.add(b) + " : " + isNonFractionalChanged(a, b));
BigDecimal bigA = new BigDecimal(Integer.MAX_VALUE + ".90");
System.out.println(bigA + " + " + b + " = " + bigA.add(b) + " : " + isNonFractionalChanged(bigA, b));
我需要一种方法将其表达为有效的 java 代码:我有 2 个 BigDecimals
,我想知道较小的 BigDecimal
是否可以(添加到较大的BigDecimal
一次) 改变较大BigDecimal
的整数部分。示例:
0.6; 0.4 ->true
0.6; 0.39 ->false
有没有有效的方法,或者我必须测试?
在意识到 Titus 的回答更简单后,我编辑了我的回答。归功于 him/her。
public static boolean integral(BigDecimal a, BigDecimal b) {
BigDecimal greater;
if (a.compareTo(b) == 1) {
greater = a;
} else {
greater = b;
}
BigDecimal sum = a.add(b);
return (sum.intValue() != greater.intValue());
}
你可以这样做:
BigDecimal b1 = new BigDecimal("0.90");
BigDecimal b2 = new BigDecimal("0.20");
if(b1.add(b2).intValue() > Math.max(b1.intValue(), b2.intValue())){
System.out.println("The integral has changed");
}else {
System.out.println("The integral is the same");
}
intValue()
方法的工作原理:
Converts this BigDecimal to an int. This conversion is analogous to the narrowing primitive conversion from double to short ... Specification: any fractional part of this BigDecimal will be discarded ...
private static boolean isNonFractionalChanged(BigDecimal a, BigDecimal b) {
BigInteger aInt = a.toBigInteger();
BigInteger aNewInt = a.add(b).toBigInteger();
return aInt.compareTo(aNewInt) != 0;
}
以上即使对于非常大的数字也适用。
BigDecimal a = new BigDecimal("0.90");
BigDecimal b = new BigDecimal("0.20");
System.out.println(a + " + " + b + " = " + a.add(b) + " : " + isNonFractionalChanged(a, b));
BigDecimal bigA = new BigDecimal(Integer.MAX_VALUE + ".90");
System.out.println(bigA + " + " + b + " = " + bigA.add(b) + " : " + isNonFractionalChanged(bigA, b));