在Java中获得两个BigDecimal值中较小者的最简单方法

Bas*_*que 0 java bigdecimal compareto

有什么方法可以确定两个BigDecimal对象中的哪一个是比(if三元运算符)调用更简单的数字(更小)BigDecimal::compareTo

鉴于:

BigDecimal x = … ;
BigDecimal y = … ;
Run Code Online (Sandbox Code Playgroud)

要么:

if( x.compareTo( y ) < 0 ) {
    return x ;
} else {
    return y ;
} 
Run Code Online (Sandbox Code Playgroud)

要么:

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. 
Run Code Online (Sandbox Code Playgroud)

小智 12

实际上,类中有一个min方法BigDecimal

BigDecimal min = x.min(y);
Run Code Online (Sandbox Code Playgroud)


Jai*_*Jai 5

API支持它。请参阅BigDecimal.min()