Java Number Divide div(Double v1, Double v2)

Here you can find the source of div(Double v1, Double v2)

Description

div

License

LGPL

Declaration

public static Double div(Double v1, Double v2) 

Method Source Code


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

import java.math.BigDecimal;

public class Main {
    private static final int DEFAULT_DIV_SCALE = 10;

    public static Double div(Double v1, Double v2) {
        return div(v1, v2, DEFAULT_DIV_SCALE);
    }/* w  w  w.  ja  v  a2  s. c o m*/

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

    private static void checkArguments(Double v1, Double v2) {
        if (v1 == null || v2 == null) {
            throw new IllegalArgumentException("caculate error: argument v1 or v2 can not be null");
        }
    }
}

Related

  1. div(Class clazz, T a, T b)
  2. div(double a, double b, int len)
  3. div(double operand1, double operand2)
  4. div(double v1, double v2)
  5. div(Double v1, Double v2)
  6. div(double v1, double v2)
  7. div(double v1, double v2, int scale)