Java BigDecimal Divide divide(BigDecimal dividend, BigDecimal divisor)

Here you can find the source of divide(BigDecimal dividend, BigDecimal divisor)

Description

Divides the given dividend by the divisor.

License

Open Source License

Parameter

Parameter Description
dividend The decimal to be divided.
divisor The decimal to divide by.

Return

The result of dividing the dividend by the divisor.

Declaration

public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor) 

Method Source Code


//package com.java2s;
/*/*from  w w w. j  av  a  2  s  .  c  o m*/
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.math.BigDecimal;

import java.math.RoundingMode;

public class Main {
    /**
     * The default rounding mode used by the methods in this class.
     */
    public static RoundingMode DEFAULT_ROUNDING_MODE = RoundingMode.HALF_UP;
    /**
     * The default decimal precision used by the methods in this class.
     */
    public static int DEFAULT_DECIMAL_PRECISION = 0;

    /**
     * Divides the given dividend by the divisor.
     *
     * @param dividend The decimal to be divided.
     * @param divisor  The decimal to divide by.
     * @return The result of dividing the dividend by the divisor.
     */
    public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor) {
        return divide(dividend, divisor, (RoundingMode) null);
    }

    /**
     * Divides the given dividend by the divisor.
     *
     * @param dividend  The decimal to be divided.
     * @param divisor   The decimal to divide by.
     * @param precision The number of decimal places preserved in the result.
     * @return The result of dividing the dividend by the divisor.
     */
    public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, int precision) {
        return divide(dividend, divisor, precision, null);
    }

    /**
     * Divides the given dividend by the divisor.
     *
     * @param dividend     The decimal to be divided.
     * @param divisor      The decimal to divide by.
     * @param roundingMode The rounding algorithm to use when rounding the result.
     * @return The result of dividing the dividend by the divisor.
     */
    public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, RoundingMode roundingMode) {
        return divide(dividend, divisor, getMaxPrecision(dividend, divisor), roundingMode);
    }

    /**
     * Divides the given dividend by the divisor.
     *
     * @param dividend     The decimal to be divided.
     * @param divisor      The decimal to divide by.
     * @param precision    The number of decimal places preserved in the result.
     * @param roundingMode The rounding algorithm to use when rounding the result.
     * @return The result of dividing the dividend by the divisor.
     */
    public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, int precision,
            RoundingMode roundingMode) {
        if (roundingMode == null)
            roundingMode = DEFAULT_ROUNDING_MODE;

        BigDecimal result = null;

        if (dividend != null && divisor != null) {
            result = dividend.divide(divisor, precision, roundingMode);
        }

        return result;
    }

    /**
     * Divides the given dividend by the divisor.
     *
     * @param dividend     The decimal to be divided.
     * @param divisor      The decimal to divide by.
     * @param precision    The number of decimal places preserved in the result.
     * @param roundingMode The rounding algorithm to use when rounding the result.
     * @return The result of dividing the dividend by the divisor.
     */
    public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, String precision,
            String roundingMode) {
        return divide(dividend, divisor, normalizePrecision(precision, dividend, divisor),
                normalizeRoundingMode(roundingMode));
    }

    /**
     * Returns the maximum precision used by the given list of decimals.
     *
     * @param decimals A list of decimals to calculate the maximum precision of.
     * @return The maximum precision used by the given list of decimals.
     */
    private static int getMaxPrecision(BigDecimal... decimals) {
        int precision = DEFAULT_DECIMAL_PRECISION;
        if (decimals != null) {
            for (BigDecimal decimal : decimals) {
                if (decimal != null && decimal.scale() > precision)
                    precision = decimal.scale();
            }
        }
        return precision;
    }

    /**
     * Returns the specified precision unless it is null, in which case the maximum precision from the list of decimals
     * is returned.
     *
     * @param precision Optional precision to be returned.
     * @param decimals  If precision not specified, the maximum precision from this list of decimals is returned.
     * @return The resolved precision to be used by the caller.
     */
    private static int normalizePrecision(String precision, BigDecimal... decimals) {
        int result;
        if (precision != null) {
            result = Integer.parseInt(precision);
        } else {
            result = getMaxPrecision(decimals);
        }
        return result;
    }

    /**
     * Returns the given rounding mode if specified, or the default rounding mode.
     *
     * @param roundingMode An optional rounding algorithm name.
     * @return The rounding algorithm to be used by the caller.
     */
    private static RoundingMode normalizeRoundingMode(String roundingMode) {
        return roundingMode == null ? DEFAULT_ROUNDING_MODE : RoundingMode.valueOf(roundingMode);
    }
}

Related

  1. divide(BigDecimal a, BigDecimal b)
  2. divide(BigDecimal amount, BigDecimal divisor)
  3. divide(BigDecimal aValue1, BigDecimal aValue2, int scale)
  4. divide(BigDecimal bd1, BigDecimal bd2)
  5. divide(BigDecimal dividend, BigDecimal divisor)
  6. divide(BigDecimal dividend, BigDecimal divisor, int scale)
  7. divide(BigDecimal left, BigDecimal right)
  8. divide(BigDecimal num1, BigDecimal num2, int scale, int mode)
  9. divide(BigDecimal num1, BigDecimal num2, int scale, int roundingMode)