Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Java divide method example

In this example Java bigdecimal class divide (BigDecimal divisor, MathContext mc) method working 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 divide method example

     

In this example Java bigdecimal class divide (BigDecimal divisor, MathContext mc) method working is demonstrated. This method divides this.object value by the specified object value & returns quotient as per defined context settings. Scale of the quotient is this.scale, which is the scale of the object in which the method is invoked.

 Example contains three MathContext class objects mc_0, mc_1 and mc_2 respectively and three different  MathContext class static fields DECIMAL32, DECIMAL64 & DECIMAL128 respectively.  Static fields used are MathContext class,  predefined objects that have their own format and rounding mode  settings defined in them.  Method throws Arithmetic Exception in following cases : 

If the Rounding mode is UNNECESSARY, ie mc.precision == 0, when the divisor value is zero, when the specified rounding mode does not represent a valid rounding mode, and when a non-terminating quotient is generated.

Method throws NumberFormatException if it finds a value other than a integer or double value. 

Syntax for using the method:

System.out.println(bigdecimal_objectName.divide(BigDecimal divisor, MathContext mc)); 
  or
BigDecimal x = this.object.divide(divisor, mc);

Java_BigDecimal_divide_BigDecimal_divisor_MathContext_mc.java

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

 public class Java_BigDecimal_divide_BigDecimal_divisor_MathContext_mc {
 public static void main(String args[]) {
 
 MathContext mc_0 = new MathContext(0),
  mc_1 = new MathContext(0),
  mc_2 = new MathContext(0);

 mc_0 = MathContext.DECIMAL32;
 mc_1 = MathContext.DECIMAL64;
 mc_2 = MathContext.DECIMAL128;
 
  
 int Dividend = 57, Divisor = 7;
 BigDecimal rose = new BigDecimal(57),
 neo = new BigDecimal(7)
 Quotient = new BigDecimal(0);
 /*The constructor BigDecimal(BigDecimal)
 is undefined*/
 
 Quotient = rose.divide(neo, mc_0);
 System.out.println("Quotient as per context settings : "
 + Quotient + "\nRemainder : " + Dividend % Divisor);
 
 Quotient = rose.divide(neo, mc_1);
 System.out.println("Result as per context settings : "
 + Quotient + "\nRemainder : " + Dividend % Divisor);
  
 Quotient = rose.divide(neo, mc_2);
 System.out.println("Result as per context settings : "
 +  Quotient + "\nRemainder : " + Dividend % Divisor);
 
 }
 

Download the code

Advertisements

Online Training Classes

Share on Google+Share on Google+

Java divide method 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 divide method example  

Post your Comment


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

Ads

 

Ads