Java BigDecimal Divide div(BigDecimal b1, BigDecimal b2)

Here you can find the source of div(BigDecimal b1, BigDecimal b2)

Description

Divide @param v1 with @param v2

License

Open Source License

Parameter

Parameter Description
b1 <code>java.math.BigDecimal</code>
b2 <code>java.math.BigDecimal</code>

Return

divided result

Declaration

public static BigDecimal div(BigDecimal b1, BigDecimal b2) 

Method Source Code


//package com.java2s;
/*/*from www.j a v  a 2s  . c om*/
 * File: $RCSfile$
 *
 * Copyright (c) 2005 Wincor Nixdorf International GmbH,
 * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of Wincor Nixdorf ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with Wincor Nixdorf.
 */

import java.math.BigDecimal;

public class Main {
    private static final int DEF_DIV_SCALE = 2;

    /**
     * Divide @param v1 with @param v2
     *
     * @param v1 <code>double</code>
     * @param v2 <code>double</code>
     * @return substract result
     */
    public static double div(double v1, double v2) {
        return div(v1, v2, DEF_DIV_SCALE);
    }

    /**
     * Divide @param v1 with @param v2
     *
     * @param b1 <code>java.math.BigDecimal</code>
     * @param b2 <code>java.math.BigDecimal</code>
     * @return divided result
     */
    public static BigDecimal div(BigDecimal b1, BigDecimal b2) {
        return div(b1, b2, DEF_DIV_SCALE);
    }

    /**
     * Divide @param v1 with @param v2 with specified scale
     *
     * @param v1    <code>double</code>
     * @param v2    <code>double</code>
     * @param scale scale for result
     * @return divided result
     */
    public static double div(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        if (v2 == 0) {
            return 0;
        }
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * Divide @param v1 with @param v2 with specified scale
     *
     * @param b1    <code>java.math.BigDecimal</code>
     * @param b2    <code>java.math.BigDecimal</code>
     * @param scale scale for result
     * @return divided result
     */
    public static BigDecimal div(BigDecimal b1, BigDecimal b2, int scale) {
        BigDecimal result = null;
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        if (b1 != null && b2 != null && b2.doubleValue() != 0) {
            result = b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP);
        } else if (b1 == null) {
            result = new BigDecimal(0);
        } else {
            result = new BigDecimal(0);
        }
        return result;
    }

    /**
     * Divide @param v1 with @param v2
     *
     * @param b1 <code>double</code>
     * @param b2 <code>double</code>
     * @return divided result           <code>java.lang.Integer</code>
     */
    public static Integer div(Integer b1, Integer b2) {
        Integer result = null;
        if (b1 != null && b2 != null && !b2.equals(new Integer("0"))) {
            int div = b1.intValue() / b2.intValue();
            result = new Integer(div);
        } else if (b1 == null) {
            result = new Integer(0);
        }
        return result;
    }

    /**
     * Divide @param v1 with @param v2
     *
     * @param b1 <code>java.lang.Long</code>
     * @param b2 <code>java.lang.Long</code>
     * @return divided result           <code>java.math.BigDecimal</code>
     */
    public static BigDecimal div(Long b1, Long b2) {
        BigDecimal result = null;
        if (b1 != null && b2 != null && !b2.equals(new Long("0"))) {
            double div = b1.doubleValue() / b2.doubleValue();
            result = new BigDecimal(div);
        } else if (b1 == null) {
            result = new BigDecimal(0);
        }
        return result;
    }
}

Related

  1. decimalDivide(BigDecimal cs, BigDecimal bcs, int scale)
  2. div(BigDecimal d1, BigDecimal d2)
  3. div(BigDecimal v1, BigDecimal v2)
  4. div(BigDecimal val, long divisor)
  5. divide(BigDecimal a, BigDecimal b)