Java Number Divide divRate(double v1, double v2)

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

Description

div Rate

License

Apache License

Declaration

public static String divRate(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 String divRate(double v1, double v2) {
        double d = div(v1, v2, DEF_DIV_SCALE);
        double r = mul(d, 100);
        return r + "%";
    }/*w ww  . j  a v  a2  s. co  m*/

    public static String divRate(double v1, double v2, int scale) {
        double d = div(v1, v2, scale);
        double r = mul(d, 100);
        return r + "%";
    }

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

    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 double mul(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2).doubleValue();
    }
}

Related

  1. divideBy(Number number1, Number number2)
  2. divideByRoundDown(Number number1, Number number2)
  3. division(Number numerator, Number denominator)
  4. divmod(byte[] number, int firstDigit, int base, int divisor)
  5. divmod256(byte[] number58, int startAt)
  6. isDivisible(Double a, int b, int decimal)