我有一个BiGDecimal price
,需要检查它是否在某个范围内。例如应该有3个条件:
if (price >= 0 and price <=500) {
....
} else if (price >=500 && price <=1000) {
....
} else if (price > 1000) {
....
}
如何使用BigDecimal类型正确地进行操作。
我有一个BiGDecimal price
,需要检查它是否在某个范围内。例如应该有3个条件:
if (price >= 0 and price <=500) {
....
} else if (price >=500 && price <=1000) {
....
} else if (price > 1000) {
....
}
如何使用BigDecimal类型正确地进行操作。
使用.compareTo()方法可以实现这一点。例如:
if ( price.compareTo( BigDecimal.valueOf( 500 ) > 0
&& price.compareTo( BigDecimal.valueOf( 1000 ) < 0 ) {
// price is larger than 500 and less than 1000
...
}
引用(和转述)JavaDoc的内容:
建议使用以下习惯用法进行比较:(x.compareTo(y) op 0),其中op是六个比较运算符之一 [(<, ==, >, >=, !=, <=)]
谢谢!
public static <T extends Comparable<T>> boolean isBetween(T value, T start, T end) {
return value.compareTo(start) >= 0 && value.compareTo(end) <= 0;
}
@Test
public void bigDecimalInRange() {
Range<BigDecimal> myRange = Range.between(BigDecimal.TEN, BigDecimal.valueOf(300));
BigDecimal tooSmallAmount = new BigDecimal("2.50");
assertFalse(myRange.contains(tooSmallAmount));
BigDecimal rightAmount = new BigDecimal("10.01");
assertTrue(myRange.contains(rightAmount));
}
我觉得这个问题没有什么变通的办法。最终你可以将其包装成一个漂亮的设计模式,但是BigDecimal只有一种比较方法。
我的想法是提取一个用于范围比较的方法:
boolean isBetween(BigDecimal value, BigDecimal min, BigDecimal max){
return value.compareTo(min) >= 0 && value.compareTo(max) <= 0;
}
使用 BigDecimal.compareTo(val)
方法来比较你的数字是大于、小于还是等于另一个数字。
返回 -1、0 或 1,表示此 BigDecimal 数值小于、等于或大于 val。
if (price.compareTo(BigDecimal.ZERO) >= 0 &&
price.compareTo(new BigDecimal(500)) <= 0) {
....
} else if (price.compareTo(new BigDecimal(500)) >= 0 &&
price.compareTo(new BigDecimal(1000)) <= 0) {
....
} else if (price.compareTo(price.compareTo(new BigDecimal(1000)) < 0) {
....
}
new BigDecimal(0)
,而是使用 BigDecimal.ZERO
。 - Luiggi MendozaBigDecimal.valueOf()
比 new BigDecimal()
更可取。 - Betlistapublic class BigDecimalDemo {
public static void main(String[] args) {
// create 2 BigDecimal objects
BigDecimal bg1, bg2;
bg1 = new BigDecimal("10");
bg2 = new BigDecimal("20");
//create int object
int res;
res = bg1.compareTo(bg2); // compare bg1 with bg2
String str1 = "Both values are equal ";
String str2 = "First Value is greater ";
String str3 = "Second value is greater";
if( res == 0 )
System.out.println( str1 );
else if( res == 1 )
System.out.println( str2 );
else if( res == -1 )
System.out.println( str3 );
}
}
资源:http://www.tutorialspoint.com/java/math/bigdecimal_compareto.htm
使用compareTo方法
java.math.BigDecimal.compareTo(BigDecimal val)
这个方法如果BigDecimal小于val,则返回-1,如果BigDecimal大于val则返回1,如果BigDecimal等于val则返回0。
BigDecimal value = BigDecimal.valueOf(2);
BigDecimal zero = BigDecimal.valueOf(0);
BigDecimal one = BigDecimal.valueOf(1);
// When it is greater than 0 it returns 1 or if it is equal to 0 it returns 0
System.out.println(value.compareTo(BigDecimal.ZERO)>=0);
// When it is greater than 1 it returns -1 or if it is equal to 1 it returns 0
System.out.println(value.compareTo(BigDecimal.ONE)<=0);
boolean condition = value.compareTo(BigDecimal.ZERO) >= 0 && value.compareTo(BigDecimal.ONE) <= 0;
System.out.println(condition);
public static <T extends Comparable<T>> boolean isBetween(T x, T lowerBound, T upperBound) {
return (x.compareTo(lowerBound) + upperBound.compareTo(x)) > 0;
}
区别在于内部求和将返回:
因此,如果总和大于0(或仅不等于0),则x变量将属于该区间。
使用 BigDecimal 类的 signum 方法:
private static boolean isBetween(BigDecimal amount, BigDecimal init, BigDecimal end) {
return amount.subtract(init).signum() >= 0 && amount.subtract(end).signum() <= 0;
}
BigDecimal.compareTo
,例如lowerBound.compareTo(value)
和upperBound.compareTo(value)
。Using BigDecimal'scompareTo
method, you can compare a BigDecimal object to another and determine if it is less than, equal to, or greater than the other object. You can use this method to comparevalue
to bothlowerBound
andupperBound
. - Jon SkeetBigInteger
... - Betlista