Java Number Divide div(Object a, Object b)

Here you can find the source of div(Object a, Object b)

Description

div

License

Open Source License

Declaration

public static Object div(Object a, Object b) 

Method Source Code


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

import java.math.RoundingMode;

public class Main {
    public static Object div(Object a, Object b) {
        if (a == null || b == null) {
            return null;
        }//w  ww  .j a  va  2s .  co m

        BigDecimal decimalA = decimal(a);
        BigDecimal decimalB = decimal(b);

        try {
            return decimalA.divide(decimalB);
        } catch (ArithmeticException ex) {
            return decimalA.divide(decimalB, 4, RoundingMode.CEILING);
        }
    }

    public static BigDecimal decimal(Object val) {
        if (val == null) {
            return null;
        }

        if (val instanceof BigDecimal) {
            return (BigDecimal) val;
        }

        if (val instanceof String) {
            return new BigDecimal((String) val);
        }

        if (val instanceof Float) {
            return new BigDecimal((Float) val);
        }

        if (val instanceof Double) {
            return new BigDecimal((Double) val);
        }

        return BigDecimal.valueOf(((Number) val).longValue());
    }
}

Related

  1. div(Double v1, Double v2)
  2. div(Double v1, Double v2)
  3. div(double v1, double v2)
  4. div(double v1, double v2, int scale)
  5. div(long v1, long v2)
  6. divide(Number a, Number b, Class cl)
  7. divide(Number n1, Number n2)
  8. divide(Number n1, Number n2)
  9. divide(Number num1, Number num2)