Java BigDecimal from toBigDecimal(Object obj)

Here you can find the source of toBigDecimal(Object obj)

Description

to Big Decimal

License

Apache License

Declaration

public static BigDecimal toBigDecimal(Object obj) 

Method Source Code

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

import java.math.BigDecimal;

public class Main {
    private static final Object NULL = null;

    public static BigDecimal toBigDecimal(Object obj) {
        if (isNull(obj)) {
            return getNull();
        }//from   ww  w. j  av  a  2  s  .c  om
        if (obj instanceof BigDecimal) {
            return (BigDecimal) obj;
        }
        if (obj instanceof Integer) {
            return new BigDecimal((Integer) obj);
        }
        if (obj instanceof Long) {
            return new BigDecimal((Long) obj);
        }
        if (obj instanceof Double) {
            return new BigDecimal((Double) obj);
        }
        if (obj.toString().matches(".*[0-9]+.*") && obj.toString().matches("[-+]?[0-9]*.[0-9]*")) {
            return new BigDecimal(obj.toString());
        }
        return getNull();
    }

    public static boolean isNull(Object obj) {
        return obj == NULL;
    }

    @SuppressWarnings("unchecked")
    public static <T> T getNull() {
        return (T) NULL;
    }
}

Related

  1. toBigDecimal(Number number)
  2. toBigDecimal(Number number, int scale)
  3. toBigDecimal(Number price)
  4. toBigDecimal(Object n)
  5. toBigDecimal(Object obj)
  6. toBigDecimal(Object object)
  7. toBigDecimal(Object val)
  8. toBigDecimal(Object value)
  9. toBigDecimal(Object value)