In this example, bigdecimal class precision method working is demonstrated. Method return type is int. Method returns the number of digits of an unscaled object value.
Java bigdecimal precision example
In this example, bigdecimal class precision method working is demonstrated. Method return type is int. Method returns the number of digits of an unscaled object value.
Method throws NumberFormatException if it finds value other than integer and double.
Syntax for using the method : public int precision()
BigDecimal objects with names x;
System.out.print(x.precision());
or
int i = x.precision();
Note: Method generates value 1 for this.object value zero.
Java_bigdecimal_precision.java
import java.math.BigDecimal;
public class Java_bigdecimal_precision {
public static void main(String args[]) {
BigDecimal honey = new BigDecimal("12"),
storm = new BigDecimal(0000);
System.out.println("honey object value :" +
" " + honey);
System.out.println("method generated " +
"result : " + honey.precision());
System.out.println("\nstorm object value" +
" : " + storm);
System.out.println("method generated " +
"result : " + storm.precision());
honey = new BigDecimal(-2321);
storm = new BigDecimal(-5.1);
System.out.println("\nhoney object value " +
": " + honey);
System.out.println("method generated " +
"result : " + honey.precision());
System.out.println("\nstorm object value " +
": " + storm);
System.out.println("method generated " +
"result : " + storm.precision());
storm = new BigDecimal(-5.1);
storm = new BigDecimal(storm.floatValue());
System.out.println("\nstorm object value " +
": " + storm);
System.out.println("method generated " +
"result : " + storm.precision());
}
}
|
Download the code