Java.math.BigDecimal.precision() Method


Advertisements


Description

The java.math.BigDecimal.precision() method returns the precision of this BigDecimal. The precision is the number of digits in the unscaled value.The precision of a zero value is 1.

Declaration

Following is the declaration for java.math.BigDecimal.precision() method.

public int precision()

Parameters

NA

Return Value

This method returns the precision of this BigDecimal object.

Exception

NA

Example

The following example shows the usage of math.BigDecimal.precision() method.

package com.tutorialspoint;

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 );
   }
}

Let us compile and run the above program, this will produce the following result −

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

java_math_bigdecimal.htm

Advertisements