BigDecimal divide(BigDecimal, RoundingMode) in Java

divide(BigDecimal, RoundingMode): This method is available in java.math.BigDecimal class of Java.

Syntax:

BigDecimal java.math.BigDecimal.divide(BigDecimal divisor, RoundingMode roundingMode)

This method takes two arguments one of type BigDecimal and another of type RoundingMode as its parameters. This method returns a BigDecimal whose value is (this /divisor), and whose scale is this.scale().

Parameters: Two parameters are required for this method.

divisor: value by which this BigDecimal is to be divided.

roundingMode: rounding mode to apply.

Returns: this / divisor.

Throws:

ArithmeticException - if divisor==0, or roundingMode==RoundingMode.UNNECESSARY and this.scale() is insufficient to represent the result of the division exactly.

Approach 1: When no exceptions.

Java

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

public class BigDecimaldivide3 {
    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("1466.788");

        BigDecimal divisor = new BigDecimal("19.9");
        RoundingMode roundingMode = RoundingMode.CEILING;
        System.out.println(bigDecimal.divide(divisor, roundingMode));
    }
}

Output:

73.708


Approach 2: ArithmeticException 

Java

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

public class BigDecimaldivide3 {
    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("1466.788");

        BigDecimal divisor = new BigDecimal("19.9");
        RoundingMode roundingMode = RoundingMode.UNNECESSARY;
        System.out.println(bigDecimal.divide(divisor, roundingMode));
    }
}


Output:

Exception in thread "main" java.lang.ArithmeticException: Rounding necessary at java.base/java.math.BigDecimal.commonNeedIncrement(BigDecimal.java:4628) at java.base/java.math.BigDecimal.needIncrement(BigDecimal.java:4684) at java.base/java.math.BigDecimal.divideAndRound(BigDecimal.java:4592) at java.base/java.math.BigDecimal.divide(BigDecimal.java:5663) at java.base/java.math.BigDecimal.divide(BigDecimal.java:1593) at java.base/java.math.BigDecimal.divide(BigDecimal.java:1674)


Some other methods of BigDecimal

BigDecimal.abs()This method returns a BigDecimal whose value is the absolute value of this BigDecimal, and whose scale is this.scale().

BigDecimal.abs(MathContext)This method returns a BigDecimal whose value is the absolute value of this BigDecimal, with rounding according to the context settings.

BigDecimal.add(BigDecimal) This method returns a BigDecimal whose value is (this +augend).

BigDecimal.add(BigDecimal, MathContext)This method returns a BigDecimal whose value is (this + augend), with rounding according to the context settings.

byteValue()This method returns the value of the specified number as a byte.

BigDecimal.byteValueExact()This method converts this BigDecimal to a byte, checking for lost information.

compareTo()This method compares this BigDecimal with the specified BigDecimal.

divide(BigDecimal)This method returns a BigDecimal whose value is (this /divisor).

divide(BigDecimal, MathContext)This method returns a BigDecimal whose value is (this /divisor), with rounding according to the context settings.

divide(BigDecimal, RoundingMode)This method returns a BigDecimal whose value is (this /divisor), and whose scale is this.scale().

divide(BigDecimal, int, RoundingMode)This method returns a BigDecimal whose value is (this /divisor), and whose scale is as specified.

divideAndRemainder(BigDecimal)This method returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of the remainder on the two operands.

divideAndRemainder(BigDecimal, MathContext)This method returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of the remainder on the two operands calculated with rounding according to the context settings.

divideToIntegralValue(BigDecimal)This method returns a BigDecimal whose value is the integer part of the quotient (this / divisor) rounded down.

divideToIntegralValue(BigDecimal ,MathContext)This method returns a BigDecimal whose value is the integer part of (this / divisor).

doubleValue()This method converts this BigDecimal to a double.

equals(Object)This method compares this BigDecimal with the specified Object for equality.

floatValue()This method converts this BigDecimal to a float.

hashCode()This method returns the hash code for this BigDecimal.

intValue()This method converts this BigDecimal to an int.

intValueExact()It converts this BigDecimal to an int, checking for lost information.

longValue(): It converts this BigDecimal to a long.

longValueExact()It converts this BigDecimal to a long, checking for lost information.

max() It returns the maximum of this BigDecimal and val.

min()It returns the minimum of this BigDecimal and val.

movePointLeft()It returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the left.

movePointRight(): It returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right.

multiply(BigDecimal)It returns a BigDecimal whose value is (this ×multiplicand), and whose scale is (this.scale() +multiplicand.scale()).

multiply(BigDecimal, MathContext)It returns a BigDecimal whose value is (this ×multiplicand), with rounding according to the context settings.

negate()It returns a BigDecimal whose value is (-this),and whose scale is this.scale().

negate(MathContext)It returns a BigDecimal whose value is (-this), with rounding according to the context settings.

plus()It returns a BigDecimal whose value is (+this), and whose scale is this.scale().

plus(MathContext)It returns a BigDecimal whose value is (+this), with rounding according to the context settings.

pow(int)It returns a BigDecimal whose value is (this^n), The power is computed exactly, to unlimited precision.

pow(int, MathContext): It returns a BigDecimal whose value is (this%^n).

precision()It returns the precision of this BigDecimal.

remainder(BigDecimal)It returns a BigDecimal whose value is (this % divisor).

remainder(BigDecimal, MathContext)It returns a BigDecimal whose value is (this %divisor), with rounding according to the context settings.

round(MathContext)It returns a BigDecimal rounded according to the MathContext settings. 

scale()It returns the scale of this BigDecimal.

scaleByPowerOfTen(int) It returns a BigDecimal whose numerical value is equal to(this * 10^n).

setScale(int)It returns a BigDecimal whose scale is the specified value, and whose value is numerically equal to this BigDecimal's.

setScale(int, RoundingMode)It returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.

shortValue(): It returns the value of the specified number as a short.

shortValueExact()It converts this BigDecimal to a short, checking for lost information.

signum()It returns the signum function of this BigDecimal.

sqrt(MathContext)It returns an approximation to the square root of this with rounding according to the context settings.

stripTrailingZeros()It returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation

subtract(BigDecimal) It returns a BigDecimal whose value is (this -subtrahend), and whose scale is max(this.scale(),subtrahend.scale()).

subtract(BigDecimal, MathContext): It returns a BigDecimal whose value is (this - subtrahend), with rounding according to the context settings.

Some constants of BigDecimal: Some constants of BigDecimal, BigDecimal.ONE, BigDecimal.ZERO and BigDecimal.TEN

toBigInteger()It converts this BigDecimal to a BigInteger.

toBigIntegerExact()It converts this BigDecimal to a BigInteger, checking for lost information. An exception is thrown if this BigDecimal has a nonzero fractional part.

toEngineeringString()It returns a string representation of this BigDecimal, using engineering notation if an exponent is needed.

toPlainString()It returns a string representation of this BigDecimal without an exponent field.

toString()It returns the string representation of this BigDecimal, using scientific notation if an exponent is needed.

ulp(): It returns the size of an ulp, a unit in the last place, of this BigDecimal.

unscaledValue()It returns a BigInteger whose value is the unscaled value of this BigDecimal. (Computes (this *10^this.scale()).)

BigDecimal.valueOf(long, int)It translates a long unscaled value and an int scale into a BigDecimal.

BigDecimal.valueOf(double): It translates a double into a BigDecimal, using the double's canonical string representation.

BigDecimal.valueOf(long)It translates a long value into a BigDecimalwith a scale of zero.

No comments:

Post a Comment