Java Double Number Divide divide(Object num1, Object num2)

Here you can find the source of divide(Object num1, Object num2)

Description

divide

License

Apache License

Declaration

public static Double divide(Object num1, Object num2) 

Method Source Code

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

import java.math.BigDecimal;

public class Main {
    private static int DEF_SCALE = 10;

    public static Double divide(Object num1, Object num2) {
        return divide(num1, num2, DEF_SCALE);
    }/*from w w  w. j a  v  a  2  s .co  m*/

    public static Double divide(Object num1, Object num2, Integer scale) {
        if (scale == null) {
            scale = DEF_SCALE;
        }
        num2 = num2 == null || Math.abs(new Double(num2.toString())) == 0 ? 1
                : num2;
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal result = bigDecimal(num1).divide(bigDecimal(num2),
                scale, BigDecimal.ROUND_HALF_UP);
        return result.doubleValue();
    }

    public static BigDecimal bigDecimal(Object object) {
        if (object == null) {
            throw new NullPointerException();
        }
        BigDecimal result;
        try {
            result = new BigDecimal(String.valueOf(object).replaceAll(",",
                    ""));
        } catch (NumberFormatException e) {
            throw new NumberFormatException("Please give me a numeral.Not "
                    + object);
        }
        return result;
    }
}

Related

  1. divide(final double n1, final double n2)
  2. divide(float a, float b)
  3. divide(float left, float right)
  4. divide(Number numerator, Number denominator)
  5. divide(Object dividend, Object divisor)
  6. divide(String num1, String num2)
  7. divide(String num1, String num2, int scale)
  8. divide(String num1, String num2, int scale)
  9. divide(String thisVal, String addVal)