ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Java BigDecimal precision() Method with Example

BigDecimal Class precision() method: Here, we are going to learn about the precision() method of BigDecimal Class with its syntax and example.
Submitted by Preeti Jain, on May 04, 2020

BigDecimal Class precision() method

  • precision() method is available in java.math package.
  • 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() 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() method does not throw an exception at the time of returning 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.

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
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.