Java中的java.math.BigDecimal.min(BigDecimal val)方法用于比较两个BigDecimal值并返回两者的最小值。
用法:
public BigDecimal min(BigDecimal val)
参数:该函数接受BigDecimal对象val作为参数,将其值与此BigDecimal对象的值进行比较,并返回最小值。
返回值:此方法返回BigDecimal,其值是此BigDecimal和val中的较小值。如果两者相等,则返回此BigDecimal。
例子:
Input : a = 17.000041900, b = 17.0000418999 Output : 17.0000418999 Input : a = 235900000146, b = 236000000000 Output : 235900000146
下面的程序将说明BigDecimal类的min()函数:
示例1:
/*Java program to illustrate
use of BigDecimal min()
function in Java */
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating 2 BigDecimal objects
BigDecimal a, b;
a = new BigDecimal("17.000041900");
b = new BigDecimal("17.0000418999");
// print the maximum value
System.out.println("Minimum Value among " + a +
" and " + b + " is " + a.min(b));
}
}
输出:
Minimum Value among 17.000041900 and 17.0000418999 is 17.0000418999
示例2:
/*Java program to illustrate
use of BigDecimal min()
to display minimum length
among two strings */
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Create 2 BigDecimal objects
BigDecimal a, b;
String s = "GeeksforGeeks";
String str = "GeeksClasses";
int l1, l2;
l1 = s.length();
l2 = str.length();
a = new BigDecimal(l1);
b = new BigDecimal(l2);
// Print the respective lengths
System.out.println("Length of string " + s + " is " + a);
System.out.println("Length of string " + str + " is " + b);
// Print the maximum value
System.out.println("Minimum length is " + a.min(b));
}
}
输出:
Length of string GeeksforGeeks is 13 Length of string GeeksClasses is 12 Minimum length is 12
参考: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#min()
相关用法
- Java BigDecimal abs()用法及代码示例
- Java BigDecimal plus()用法及代码示例
- Java BigDecimal max()用法及代码示例
- Java BigDecimal ulp()用法及代码示例
- Java BigDecimal longValue()用法及代码示例
- Java BigDecimal intValue()用法及代码示例
- Java BigDecimal precision()用法及代码示例
- Java BigDecimal longValueExact()用法及代码示例
- Java BigDecimal intvalueExact()用法及代码示例
- Java BigDecimal hashCode()用法及代码示例
- Java BigDecimal movePointLeft()用法及代码示例
- Java BigDecimal pow()用法及代码示例
- Java BigDecimal movePointRight()用法及代码示例
- Java BigDecimal round()用法及代码示例
- Java BigDecimal scale()用法及代码示例
注:本文由纯净天空筛选整理自RICHIK BHATTACHARJEE大神的英文原创作品 BigDecimal min() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。