Java Number Divide div(double v1, double v2)

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

Description

div

License

Apache License

Declaration

public static double div(double v1, double v2) 

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 = 10;

    public static double div(double v1, double v2) {
        return div(v1, v2, DEF_DIV_SCALE);
    }/* w w  w  . j a v  a  2  s . co m*/

    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");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static float div(float v1, float v2) {
        return div(v1, v2, DEF_DIV_SCALE);
    }

    public static float div(float v1, float v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(Float.toString(v1));

        BigDecimal b2 = new BigDecimal(Float.toString(v2));
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).floatValue();
    }
}

Related

  1. div(double a, double b, int len)
  2. div(double operand1, double operand2)
  3. div(Double v1, Double v2)
  4. div(double v1, double v2)
  5. div(Double v1, Double v2)
  6. div(double v1, double v2, int scale)
  7. div(long v1, long v2)
  8. div(Object a, Object b)
  9. divide(Number a, Number b, Class cl)