當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java BigDecimal min()用法及代碼示例


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()



相關用法


注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 BigDecimal min() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。