6

Is there some way to determine which of two BigDecimal objects is the lower (smaller) number that is simpler than an if or a ternary operator calling BigDecimal::compareTo?

Given:

BigDecimal x = … ;
BigDecimal y = … ;

Either:

if( x.compareTo( y ) < 0 ) {
    return x ;
} else {
    return y ;
} 

Or:

BigDecimal lower = ( x.compareTo( y ) < 0 ) ? x : y ;  // If x is smaller than y, use x. If x is greater than or equal to y, use y. 
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154

2 Answers2

22

Actually, there's a min method in the BigDecimal class.

BigDecimal min = x.min(y);
Bruno L
  • 839
  • 5
  • 9
7

The API supports it. See BigDecimal.min().

Jai
  • 8,165
  • 2
  • 21
  • 52