int precision()

优质
小牛编辑
137浏览
2023-12-01

描述 (Description)

java.math.BigDecimal.precision()方法返回此BigDecimal的精度。 精度是未缩放值中的位数。零值的精度为1。

声明 (Declaration)

以下是java.math.BigDecimal.precision()方法的声明。

public int precision()

参数 (Parameters)

NA

返回值 (Return Value)

此方法返回此BigDecimal对象的精度。

异常 (Exception)

NA

例子 (Example)

以下示例显示了math.BigDecimal.precision()方法的用法。

package cn.xnip;
import java.math.*;
public class BigDecimalDemo {
   public static void main(String[] args) {
      // create 2 BigDecimal Objects
      BigDecimal bg1, bg2;
      bg1 = new BigDecimal("123.234");
      bg2 = new BigDecimal("523");
      // create 2 int objects
      int i1, i2;
      // assign the result of precision of bg1, bg2 to i1 and i2
      i1 = bg1.precision();
      i2 = bg2.precision();
      String str1 = "The precision of " + bg1 + " is " + i1;
      String str2 = "The precision of " + bg2 + " is " + i2;
      // print the values of i1, i2
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

The precision of 123.234 is 6
The precision of 523 is 3