Java中的java.math.BigDecimal.max(BigDecimal val)方法用於比較兩個BigDecimal值,並返回兩個最大值。這與Java中的BigDecimal max()方法相反。
用法:
public BigDecimal max(BigDecimal val)
參數:該函數接受BigDecimal對象val作為參數,將其值與此BigDecimal對象的值進行比較,並返回最大值。
返回值:此方法返回BigDecimal,其值大於此BigDecimal和val中的較大者。如果兩者相等,則返回此BigDecimal。
例子:
Input : a = 17.000041900, b = 17.0000418999 Output : 17.000041900 Input : a = 235900000146, b = 236000000000 Output : 236000000000
下麵的程序將說明BigDecimal類的max()函數。
示例1:
// Java program to illustrate use of
// BigDecimal max() function in Java
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// create 2 BigDecimal objects
BigDecimal a, b;
a = new BigDecimal("235900000146");
b = new BigDecimal("236000000000");
// print the maximum value
System.out.println("Maximum Value among " + a +
" and " + b + " is " + a.max(b));
}
}
輸出:
Maximum Value among 235900000146 and 236000000000 is 236000000000
示例2:
// Java program to illustrate use of BigDecimal max()
// to display maximum length among two strings in Java
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("Maximum length is " + a.max(b));
}
}
輸出:
Length of string GeeksforGeeks is 13 Length of string GeeksClasses is 12 Maximum length is 13
參考: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#max()
相關用法
- Java BigDecimal abs()用法及代碼示例
- Java BigDecimal plus()用法及代碼示例
- Java BigDecimal min()用法及代碼示例
- 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 max() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。