Java BigDecimal divide examples

In this example, Java bigdecimal class divide(BigDecimal divisor, int scale, int roundingMode) method working is demonstrated. Example contains scale feature which is an integer type value that defines number of digits after decimal.

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 divide examples

     

In this example, Java bigdecimal class divide(BigDecimal divisor, int scale, int roundingMode) method working  is demonstrated. Example contains scale feature which is an integer type value that defines number of digits after decimal. Here the scale of the quotient will be specified in the program.  Also in the example, bigdecimal class ROUND_FLOOR  field is used that defines the Rounding mode to round towards negative infinity. Method throws Arithmetic Exception in following cases when the specified Rounding mode is ROUND_UNNECESSARY,
when the divisor value is zero and when the specified rounding mode does not represent a valid rounding mode.

With divisor value zero, the rounding mode becomes equal to ROUND_UNNECESSARY therefore method throws Arithmetic Exception , when the divisor value is zero.

Method also throws IllegalArgumentException in the case when the specified rounding mode is not a valid rounding mode.

Syntax for using the method:

public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)   

bigdecimal_objectName = 
bigdecimal_objectDividendName.bigdecimal.divide(bigdecimal_objectDivisorName, int scale, int roundingMode);

Java_BigDecimal_divide_int_scale_int_roundingMode.java

import java.math.BigDecimal;
import java.text.NumberFormat;

public class Java_BigDecimal_divide_int_scale_int_roundingMode {
  public static void main(String args[]) {

  System.out.println("Actual PI value with no " +
  "scale defined : " + (double22 7);

  int i = 22, j = 7;
  BigDecimal a = new BigDecimal(i);
  BigDecimal b = new BigDecimal(j),
  c = new BigDecimal(0);

  c = a.divide(b, 5, BigDecimal.ROUND_FLOOR);
  System.out.println("PI value with scale 5 : " 
  + c);

  BigDecimal m = new BigDecimal(i);
  BigDecimal n = new BigDecimal(j),
  o = new BigDecimal(0);

  // Creating NumberFormat class num object
  NumberFormat num = NumberFormat.getInstance();

  // Invoking .setMinimumFraction() in object num
  num.setMinimumFractionDigits(5);
  System.out.println("Formated  PI value : "
  + num.format(c));

  /*
 * Here an Arithmetic Exception is thrown by method
 
 * i = 22; j = 0; 
 * BigDecimal x = new BigDecimal(i);
 * BigDecimal y = new BigDecimal(j),
 * z = new BigDecimal(0);
 
 * z = x.divide(y, 4, BigDecimal.ROUND_FLOOR);
 * System.out.println("PI value with scale 4 : "
 *  + c); 
 */
  }
}

Download the source code

Advertisements

Ads
Share on Google+Share on Google+

Java BigDecimal divide examples

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 divide examples   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
prachi
June 1, 2011
bigdecimal

division eg

Ads

 

Ads