Java BigDecimal byteValueExact example

With this example, working of byteValueExact() method in getting exact byte value from a bigdecimal object is demonstrated.

Ads

Tutorials   
Java Bigdecimal Java bigdecimal toBigIntegerExact example Java bigdecimal shortValue example Java bigdecimal shortValueExact Java bigdecimal signum example Java bigdecimal stripTrailingZeros example Java bigdecimal subtract Java bigdecimal subtract method example Java bigdecimal toBigInteger example Java bigdecimal toEngineeringString example Java bigdecimal toPlainString example Java bigdecimal toString example Java bigdecimal ulp example Java bigdecimal unscaledValue example Java bigdecimal valueOf double example Java bigdecimal valueOf long example Java bigdecimal valueOf long method Java bigdecimal setScale method example Java bigdecimal setScale example Java bigdecimal scaleByPowerOfTen example Java bigdecimal scale example Java bigdecimal round example Java bigdecimal remainder method example Java bigdecimal remainder example Java bigdecimal precision example Java bigdecimal pow method example Java bigdecimal pow example Java bigdecimal plus method example Java bigdecimal plus example Java bigdecimal negate method example Java bigdecimal negate example Java bigdecimal multiply method example Java bigdecimal multiply example Java BigDecimal movePointRight example Java bigdecimal movePointLeft example Java bigdecimal min example Java bigdecimal max example Java BigDecimal divideAndRemainder example Java BigDecimal divideAndRemainder method example Java BigDecimal divide examples Java BigDecimal divideToIntegral example Java BigDecimal divideToIntegral method example Java BigDecimal doubleValue example Java BigDecimal equals example Java BigDecimal hashCode example Java BigDecimal intValue example Java BigDecimal intValueExact example Java BigDecimal longValue example Java BigDecimal longValueExact example Java BigDecimal abs example
Ads

Java BigDecimal byteValueExact example

     


With this example, working of byteValueExact() method in getting exact byte value from a bigdecimal object is demonstrated. This method transfigures(changes) a bigdecimal object value into a byte data type value.

Method throws Arithmetic Exception in following two cases. 

Firstly when the fractional part value of bigdecimal object is nonzero. Secondly if any how the value does not fit in byte,  may be due to over flowing case in which the number surpasses (exceeds) the defined byte range ( -128 to 127). 

Syntax for using byteValueExact() method: public byte byteValueExact()
  byte_VariableName = bigdecimal_objectName.byteValueExact();

In bigdecimal to byte transformation, sometimes the bigdecimal values needs rounding, as byte can hold only non-decimal values. This is necessary because as mentioned above, method throws Arithmetic Exception while converting a bigdecimal value that has a non-zero fractional part.

Java_BigDecimal_byteValueExact.java

import java.math.BigDecimal;
import java.math.*;

public class Java_BigDecimal_byteValueExact {
  public static void main(String args[]) {
  double rose = 246.0;
  // byte range -128 to 127

  BigDecimal jack = new BigDecimal(rose);

  // Rounding is necessary as byte can not hold decimal values
  System.out.println("This value does not fit into byte "
  "data type range so its printing will" +
  " generate Arithmetic Exception");
  System.out.println("BigDecimal value : "
  + jack.doubleValue());
  System.out.println("rounded value : "
  + Math.round(jack.doubleValue()));

  /* With the below print statement an 
  ArithmeticException is generated
  because the value does not fit into byte range
  
  System.out.println("byte converted value :" 
  + jack.byteValueExact());
  */

  jack = new BigDecimal("-128.0564000");
  System.out.println("\n\nBigDecimal value : " + jack);

  long roundedValue = Math.round(jack.doubleValue());
  System.out.println("Rounded value : " + roundedValue);

  jack = new BigDecimal(roundedValue);
  System.out.println("Rounding is necessary as the fractional "
  "part of number is not zero");
  System.out.println("byte converted value : " 
  + jack.byteValueExact());

  }
}

Download the source code

Advertisements

Ads
Share on Google+Share on Google+

Java BigDecimal byteValueExact example

Posted on: November 21, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Java BigDecimal byteValueExact example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads