BigDecimal类precision()方法 (BigDecimal Class precision() method)
precision() method is available in java.math package.
precision()方法在java.math包中可用。
precision() method is used to get the precision of this BigDecimal object and we all know the term precision is the number of digits represented in the un-scaled value.
precision()方法用于获取此BigDecimal对象的精度,我们都知道精度一词是未缩放值中表示的位数。
precision() 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.
precision()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。
precision() method does not throw an exception at the time of returning precision.
precision()方法在返回precision时不会引发异常。
Syntax:
句法:
public int precision();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of this method is int, it returns the precision of this BigDecimal object.
此方法的返回类型为int ,它返回此BigDecimal对象的精度。
Example:
例:
// Java program to demonstrate the example
// of int precision() method of BigDecimal
import java.math.*;
public class PrecisionOfBD {
public static void main(String args[]) {
// Initialize two variables and
// first is of "short" and second is
// of "String" type
short val1 = 1321;
String val2 = "100.24";
// Initialize two BigDecimal objects
BigDecimal b_dec1 = new BigDecimal(val1);
BigDecimal b_dec2 = new BigDecimal(val2);
// By using precision() method - is to
// return the precision of this BigDecimal
// b_dec1 and precision is the number of
// digits in unscaled value i.e. 1321 so
// it returns 4 because total number of
// digits in this BigDecimal (1321) is 4
int precision = b_dec1.precision();
System.out.println("b_dec1.precision(): " + precision);
// By using precision() method - is to
// return the precision of this BigDecimal
// b_dec2 and precision is the number of
// digits in unscaled value i.e. 100.24 so
// it returns 5 because total number of
// digits in this BigDecimal (100.24) is 5
precision = b_dec2.precision();
System.out.println("b_dec2.precision(): " + precision);
}
}
Output
输出量
b_dec1.precision(): 4
b_dec2.precision(): 5
翻译自: https://www.includehelp.com/java/bigdecimal-precision-method-with-example.aspx