Check if BigDecimal value is in range

40.9k Views Asked by At

I have BiGDecimal price and i need to check if it is in some range. For example should be 3 conditions:

if (price >= 0 and price <=500) {
   ....
} else if (price >=500 && price <=1000) {
   ....
} else if (price > 1000) {
   ....
}

How to do it right using BigDecimal type.

10

There are 10 best solutions below

1
On BEST ANSWER

That is achievable using the .compareTo() method. For instance:

if ( price.compareTo( BigDecimal.valueOf( 500 ) > 0 
     && price.compareTo( BigDecimal.valueOf( 1000 ) < 0 ) {
    // price is larger than 500 and less than 1000
    ...
}

Quoting (and paraphrasing) from the JavaDoc:

The suggested idiom for performing these comparisons is: (x.compareTo(y) op 0), where op is one of the six comparison operators [(<, ==, >, >=, !=, <=)]

Cheers,

4
On

Use BigDecimal.compareTo(val) to compare if your Number is bigger, smaller or equal.

returns -1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than 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) {
   ....
}
0
On

use compareTo method

java.math.BigDecimal.compareTo(BigDecimal val)

This method returns -1 if the BigDecimal is less than val, 1 if the BigDecimal is greater than val and 0 if the BigDecimal is equal to val

2
On

There is no workaround to this i think. Eventually you could wrap it into a nice design pattern, but BigDecimal has only one method for comparing.

My idea is to extract a method for range:

boolean isBetween(BigDecimal value, BigDecimal min, BigDecimal max){
  return value.compareTo(min) >= 0 && value.compareTo(max) <= 0;
}
0
On

You can do sthg like this;

public 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 );
 }
}

resource : http://www.tutorialspoint.com/java/math/bigdecimal_compareto.htm

0
On

Lets make it generic:

public static <T extends Comparable<T>> boolean isBetween(T value, T start, T end) {
    return value.compareTo(start) >= 0 && value.compareTo(end) <= 0;
}
0
On

As said by Kirill Karandin, you can write a generic method:

public static <T extends Comparable<T>> boolean isBetween(T x, T lowerBound, T upperBound) {
    return (x.compareTo(lowerBound) + upperBound.compareTo(x)) > 0;
}

The difference is that the inner sum will return:

  • 0, if x is either less than of min or greater than of max;
  • 1, if x is equal to either min or max;
  • 2, if x belong to the interval, but it's not equal to either min or max.

Consequently, if the sum is greater than of 0 (or simply not equal to 0) the x variable will belong to the interval.

0
On

If you already use Apache commons lang, one option is to use the Range class instead of writing your own utility method:

@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));
}
0
On
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);
0
On

Use signum method from BigDecimal class:

private static boolean isBetween(BigDecimal amount, BigDecimal init, BigDecimal end) {
    return amount.subtract(init).signum() >= 0 && amount.subtract(end).signum() <= 0;
}