Java BigDecimal Divide div(BigDecimal d1, BigDecimal d2)

Here you can find the source of div(BigDecimal d1, BigDecimal d2)

Description

div

License

Apache License

Declaration

public static BigDecimal div(BigDecimal d1, BigDecimal d2) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.math.BigDecimal;

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

    public static double div(double d1, double d2) {
        return div(d1, d2, DEF_DIV_SCALE);
    }// ww w .  j  a  v  a 2s. c  o m

    public static BigDecimal div(BigDecimal d1, BigDecimal d2) {
        return div(d1, d2, DEF_DIV_SCALE);
    }

    public static double div(double d1, double d2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(d1));
        BigDecimal b2 = new BigDecimal(Double.toString(d2));
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static BigDecimal div(BigDecimal d1, BigDecimal d2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        return d1.divide(d2, scale, BigDecimal.ROUND_HALF_UP);
    }
}

Related

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