Java Number Divide isDivisible(Double a, int b, int decimal)

Here you can find the source of isDivisible(Double a, int b, int decimal)

Description

is Divisible

License

Open Source License

Declaration

public static boolean isDivisible(Double a, int b, int decimal) 

Method Source Code


//package com.java2s;
import java.math.BigDecimal;

public class Main {
    public static boolean isDivisible(Double a, int b, int decimal) {
        boolean result = false;
        Double divideValue = divide(a.toString(), String.valueOf(b), 5, BigDecimal.ROUND_HALF_UP).doubleValue();
        String divideStr = divideValue.toString();
        String remainder = divideStr.substring(divideStr.lastIndexOf(".") + 1);
        if ("0".equals(remainder)) {
            result = true;/* w ww. j a v  a  2s.  c o  m*/
        } else {
            result = remainder.length() == decimal;
        }
        return result;
    }

    /**
     * Double divide
     * @param a
     * @param b
     * @return
     * @Create Sin@2012/2/23
     */
    public static BigDecimal divide(String a, String b, int scale, int roundMode) {
        BigDecimal bd1 = new BigDecimal(a);
        BigDecimal bd2 = new BigDecimal(b);
        return bd1.divide(bd2, scale, roundMode);
    }

    public static Double divide(Double a, Double b, int scale, int roundMode) throws Exception {
        BigDecimal bd1 = new BigDecimal(a.toString());
        BigDecimal bd2 = new BigDecimal(b.toString());
        return bd1.divide(bd2, scale, roundMode).doubleValue();
    }

    public static Double divide(Double a, Double b) {
        BigDecimal bd1 = new BigDecimal(a.toString());
        BigDecimal bd2 = new BigDecimal(b.toString());
        return bd1.divide(bd2).doubleValue();
    }
}

Related

  1. divideByRoundDown(Number number1, Number number2)
  2. division(Number numerator, Number denominator)
  3. divmod(byte[] number, int firstDigit, int base, int divisor)
  4. divmod256(byte[] number58, int startAt)
  5. divRate(double v1, double v2)