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

Java BigDecimal divideAndRemainder method example

In the example method of divideAndRemainder(BigDecimal divisor, MathContext mc) returns 'quotient' as per context settings defined in MathContext class object 'mc'.

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 divideAndRemainder method example

     

In the example method of divideAndRemainder(BigDecimal divisor, MathContext mc) returns 'quotient'  as per context settings defined in MathContext class object 'mc'.  

Method divides this.object value by the value of object passed. Method return type is an bigdecimal array of length two(2). This array contains the method generated results. At array index zero(array[0]) method generated first result, 'quotient' is located & at array index one(array[1])  method generated second result, 'remainder' is located. 

The first result 'quotient' will always possess integer format. This means method will only generate the integer  part, i.e. the portion of 'quotient' before decimal. Method will generate the remainder also as the second(array[1]) element of bigdecimal array[]. 

Method throws Arithmetic Exception when the divisor value is equal to zero.

Note : Using context settings makes no effect in the generating results as methods neglects decimal part of the answer, thus no decimal part therefore no rounding behavior required hence no context settings required.

Syntax for using the method:
public BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc)

bigdecimal_objectName = dividendName.divideAndRemainder(divisor, mc)

Java_divideAndRemainder_BigDecimal_divisor_MathContext_mc.java

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;

public class Java_divideAndRemainder_BigDecimal_divisor_MathContext_mc {
  public static void main(String args[]) {
  System.out.println("Java BigDecimal class "
  "\n\tdivideAndRemainder(BigDecimal divisor," +
  " MathContext mc) method example\n");
  int int_value = 25;
  long long_value = 78125;

  MathContext mc = new MathContext(0);
  /* Specifying a static object 'DECIMAL128' in object 
  //'mc' contains
  // following precision settings
  // Number of digits to be accepted : 34
 Rounding mode : HALF_EVEN.*/

  mc = MathContext.DECIMAL128;

  BigDecimal obj_0 = new BigDecimal(long_value),
  obj_1 = new BigDecimal(int_value);

  BigDecimal ray[] = obj_0.divideAndRemainder(obj_1, mc);

  System.out.println("BigDecimal Array ray[] result");
  System.out.println("Quotient : " + obj_0.divide(obj_1)
  "\nQuotient's integer part : " + ray[0
  "\nRemainder : " + ray[1]);

  int_value = 10;
  long_value = 466;
  NumberFormat nc = NumberFormat.getInstance();
  nc.setMinimumFractionDigits(3);
  BigDecimal silver = new BigDecimal(long_value),
  surfer = new BigDecimal(int_value)
  galactus[] = silver.divideAndRemainder(surfer);
  System.out.println("\nBigDecimal Array galactus[] result");
  System.out.println("Quotient : " + nc.format(silver.divide(surfer))
  "\nQuotient's integer part : " + galactus[0]
  "\nRemainder : " + galactus[1]);

  }
}

Download the code

Advertisements

Share on Google+Share on Google+

Java BigDecimal divideAndRemainder 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

Ads
 

Discuss: Java BigDecimal divideAndRemainder method example  

Post your Comment


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

Ads

 

Ads