BigDecimal类intValueExact()方法 (BigDecimal Class intValueExact() method)
intValueExact() method is available in java.math package.
intValueExact()方法在java.math包中可用。
intValueExact() method is used to convert this BigDecimal into an exact integer value.
intValueExact()方法用于将此BigDecimal转换为精确的整数值。
intValueExact() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
intValueExact()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
intValueExact() method may throw an exception at the time of converting BigDecimal to an int.
在将BigDecimal转换为int时, intValueExact()方法可能会引发异常。
ArithmeticException: This exception may throw when this BigDecimal holds fractional part that is other than 0 or the resulting value is not enough to adjust in an int.
ArithmeticException :当此BigDecimal持有非0的小数部分或结果值不足以int进行调整时,可能引发此异常。
Syntax:
句法:
public int intValueExact();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of this method is int, it returns the exact integer representation of this BigDecimal.
此方法的返回类型为int ,它返回此BigDecimal的确切整数表示形式。
Example:
例:
// Java program to demonstrate the example
// of int intValueExact() method of BigDecimal
import java.math.*;
public class IntValueExactOfBD {
public static void main(String args[]) {
// Instantiates two variables val
// and str
long val = 124589425l;
String str = "100";
// Initialize two BigDecimal objects
BigDecimal b_dec1 = new BigDecimal(val);
BigDecimal b_dec2 = new BigDecimal(str);
// to convert this BigDecimal b_dec1
// into a int, and store in a variable named i_conv
int i_conv = b_dec1.intValueExact();
System.out.println("b_dec1.intValueExact(): " + i_conv);
// to convert this BigDecimal b_dec2
// into a int, and store in a variable named i_conv
i_conv = b_dec2.intValueExact();
System.out.println("b_dec2.intValueExact(): " + i_conv);
}
}
Output
输出量
b_dec1.intValueExact(): 124589425
b_dec2.intValueExact(): 100
翻译自: https://www.includehelp.com/java/bigdecimal-intvalueexact-method-with-example.aspx