Home Java Java-bigdecimal Java BigDecimal intValueExact example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java BigDecimal intValueExact example

Advertisement
Java bigdecimal class intValueExact() method transforms bigdecimal value exactly in to integer type values.

Java BigDecimal intValueExact example

     

Java bigdecimal class intValueExact() method transforms bigdecimal value exactly in to integer type values. Method throws NumberFormatException if it value other than a integer or double. And also throws an ArithmeticException when rounding is necessary. 
In the example four bigdecimal  objects namely: ganymede_0, ganymede_1, ganymede_2 & ganymede_3 respectively  have been created. 
In the example MathContext class object mc is created for rounding the objects value. 

In the example along with method generated result, original BigDecimal value is also shown.

Syntax for using the method: public int intValueExact()
System.out.println(bigdecimal_objectName.intValueExact()); 
or
int i = (this.object).intValueExact();

Java_BigDecimal_intValueExact.java

import java.math.BigDecimal;
import java.math.MathContext;


public class Java_BigDecimal_intValueExact {
  public static void main(String args[]) {
  
  MathContext mc = new MathContext(BigDecimal.ROUND_DOWN);
  
  BigDecimal ganymede_0 = new BigDecimal(2.33544, mc),
  ganymede_1 = new BigDecimal(43.45654, mc),
  ganymede_2 = new BigDecimal(125.5455, mc),
  ganymede_3 = new BigDecimal(2.58555, mc);  
  
  
  BigDecimal ID[] =
  {ganymede_0, ganymede_1, ganymede_2, ganymede_3};
  System.out.println("BigDecimal objects values \n'" +
  "ganymede_0 '\nvalue : " + ID[0]
  +"\nExact integer value : " + ID[0].intValueExact());
  
  System.out.println("\n'ganymede_1 '\nvalue : " + ID[1]
  +"\nExact integer value : " + ID[1].intValueExact());

  System.out.println("\n'ganymede_2 '\nvalue : " + ID[2]
  +"\nExact integer value : " + ID[2].intValueExact());

  System.out.println("\n'ganymede_3 '\nvalue : " + ID[3]
  +"\nExact integer value : " + ID[3].intValueExact());
  }
}

Download the code

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 21, 2008

Ask Questions?    Discuss: Java BigDecimal intValueExact example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com