Java Data Type Tutorial - Java BigDecimal.precision()








Syntax

BigDecimal.precision() has the following syntax.

public int precision()

Example

In the following code shows how to use BigDecimal.precision() method.

//  www. j  a  va  2  s . c om

import java.math.BigDecimal;

public class Main {

  public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123.234");
    BigDecimal bg2 = new BigDecimal("523");

    // assign the result of precision of bg1, bg2 to i1 and i2
    int i1 = bg1.precision();
    int 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 code above generates the following result.